Shell, init files, variables, and expansions

Shell, init files, variables, and expansions

This project, "Shell, init files, variables, and expansions," provides a comprehensive guide to working with the shell environment in Linux.

·

16 min read

prerequisites

  • Allowed editors: vi, vim, emacs

  • All your scripts will be tested on Ubuntu 20.04 LTS

  • All your scripts should be exactly two lines long ($ wc -l file should print 2)

  • All your files should end with a new line (why?)

  • The first line of all your files should be exactly #!/bin/bash

  • A README.md file, at the root of the folder of the project, describing what each script is doing

  • You are not allowed to use &&, || or ;

  • You are not allowed to use bc, sed or awk

  • All your files must be executable

    overview

The "Shell, init files, variables, and expansions" project is a continuation of the previous tutorial on the shell, which covered shell navigation, shell permissions, and shell I/O filter redirection. In this project, you will learn about various aspects of shell scripting that are essential for working effectively in a command-line environment.

The first topic covered in this project is init files. You will learn about the /etc/profile file and the /etc/profile.d directory, which contain global settings for all users on a system. You will also learn about the ~/.bashrc file, which contains user-specific settings.

The next topic covered is variables. You will learn about the difference between local and global variables, as well as reserved variables that are used by the shell. You will also learn how to create, update, and delete shell variables.

Expansions are another important topic covered in this project. You will learn about different types of expansions, including parameter expansion, command substitution, and arithmetic expansion. You will also learn how to use quotes properly and perform arithmetic operations with the shell.

Additionally, you will learn how to create aliases and temporarily disable them, execute commands from a file in the current shell, and create shell functions. These topics will help you work more efficiently and customize your shell environment to your specific needs.

Overall, this project provides a comprehensive overview of essential shell scripting topics that are essential for any command-line user. By mastering the concepts covered in this project, you will be able to work more efficiently and customize your shell environment to suit your specific needs.

introduction

Init files, variables, and expansions are all concepts related to computer programming and shell scripting. Here's an explanation of each:

  1. Init files:

Init files are configuration files that are executed automatically when a shell starts up. They typically contain commands that set environment variables, define aliases, and configure other aspects of the shell's behavior. In Unix-like operating systems, there are several init files that can be used, including .bashrc, .bash_profile, and .profile.

  1. Variables:

Variables are named containers that hold a value or reference to a value. They are used to store and manipulate data in computer programs. In shell scripting, variables are typically created using the syntax "variable_name=value". The value can be a string, number, or another data type.

  1. Expansions:

Expansions are a way of manipulating variables and other data in shell scripts. They are used to extract specific parts of a variable's value, perform arithmetic operations, and more. Some common types of expansions include parameter expansion, command substitution, and arithmetic expansion.

Parameter expansion is used to manipulate the value of a variable by adding prefixes or suffixes, removing substrings, or performing other transformations. For example, the expression "${variable_name%suffix}" would remove the suffix from the value of the variable.

Command substitution allows the output of a command to be used as a variable's value. For example, the expression "$(ls)" would substitute the output of the "ls" command as the value of the variable.

Arithmetic expansion allows mathematical operations to be performed on the value of a variable. For example, the expression "$((2+2))" would evaluate to the value 4.

hence, init files, variables, and expansions are all important concepts in shell scripting and programming that allow for customization and flexibility in executing commands and manipulating data.

GENERAL

What happens when you type $ ls -l *.txt

When you type "$ ls -l .txt" in a shell, the shell first expands the ".txt" wildcard expression to a list of all files in the current directory that have a ".txt" extension. It then passes the resulting list of filenames as arguments to the "ls" command, along with the "-l" option.

Here's an example of how this would look in a Unix shell:

diffCopy code$ ls -l *.txt
-rw-r--r-- 1 user user  168 Apr  1 14:23 file1.txt
-rw-r--r-- 1 user user  352 Apr  2 10:11 file2.txt
-rw-r--r-- 1 user user  256 Apr  3 09:14 file3.txt

In this example, the "ls" command is executed with the "-l" option, which tells it to display the files in a long format that includes additional information such as the file's permissions, owner, and creation date. The "*.txt" expression is expanded to a list of all files in the current directory that have a ".txt" extension, which in this case are "file1.txt", "file2.txt", and "file3.txt".

The output of the "ls" command shows the information for each of the matched files in the directory, including their names, sizes, permissions, and other attributes. The "-l" option causes the output to be displayed in a long format, with each file's information shown on a separate line.

In summary, when you type "$ ls -l *.txt", the shell expands the wildcard expression to a list of matching files and passes them as arguments to the "ls" command, which displays detailed information about each file in the list.

SHELL INITIALIZATION FILES

What are the /etc/profile file and the /etc/profile.d directory What is the ~/.bashrc file

  1. /etc/profile file and /etc/profile.d directory:

The /etc/profile file is a system-wide configuration file for the Bourne-Again SHell (bash) on Unix-like systems. It is executed whenever a new login shell is started for any user on the system, and it sets system-wide environment variables and aliases that apply to all users.

The /etc/profile.d directory is a directory where additional shell scripts can be placed that is also executed when a new login shell is started. Each script in the directory should be named with a ".sh" extension and contain a valid shell code. This allows system administrators to add customizations to the system-wide environment without modifying the /etc/profile file directly.

  1. ~/.bashrc file:

The ~/.bashrc file is a user-specific configuration file for the Bourne-Again SHell (bash) on Unix-like systems. It is executed whenever a new non-login shell is started for the user, such as when a terminal window is opened.

Here's an example of what a simple ~/.bashrc file might look like:

# Set an alias for long-format directory listings
alias ll='ls -alF'

# Set the default editor to nano
export EDITOR=nano

# Add a custom directory to the PATH variable
export PATH="$PATH:/usr/local/bin"

In this example, the file sets an alias for the "ls" command that displays files in a long format, sets the default text editor to nano, and adds a custom directory to the user's PATH environment variable.

When the user opens a new terminal window, the ~/.bashrc file is executed and the customizations take effect for that session.

Therefore, both the /etc/profile file and the ~/.bashrc file are important configuration files for the bash shell on Unix-like systems, allowing for system-wide and user-specific customizations to be applied.

VARIABLES

What is the difference between a local and a global variable

In programming, a local variable is a variable that is defined within a specific function or block of code, and can only be accessed within that function or block. A global variable, on the other hand, is a variable that is defined outside of any function or block of code, and can be accessed from anywhere in the program.

In shell scripting, local and global variables behave similarly. A local variable is a variable that is defined within a function, and can only be accessed within that function. A global variable is a variable that is defined outside of any function or block of code, and can be accessed from anywhere in the script.

What is a reserved variable?

In shell scripting, there are several reserved variables that have special meanings or functions within the shell. These include variables such as $HOME, which contains the current user's home directory, and $PATH, which contains a list of directories to search for executable files.

How to create, update and delete shell variables

In shell scripting, variables can be created by simply assigning a value to them using the "=" operator. For example:

myvar="hello world"

To update the value of a variable, simply assign a new value to it:

myvar="new value"

To delete a variable, use the "unset" command:

unset myvar

This will remove the "myvar" variable from the shell's environment.

Thats, understanding the differences between local and global variables, knowing the reserved variables in shell scripting, and knowing how to create, update, and delete shell variables are important skills for writing effective shell scripts.

What are the roles of the following reserved variables: HOME, PATH, PS1

  • HOME: Contains the path to the current user's home directory.

  • PATH: Contains a list of directories to search for executable files. When a user enters a command in the shell, the shell looks in each directory listed in PATH for an executable file with the name of the command.

  • PS1: Defines the primary prompt string that appears in the shell. This variable can be customized to display information such as the current directory, username, or hostname.

  1. what is Special parameters?

Special parameters are variables that are set automatically by the shell and have special meanings or functions within the shell. These variables cannot be directly modified like regular variables.

Some examples of special parameters include:

  • $0: The name of the shell or script.

  • $1, $2, $3, etc.: The positional parameters, representing the arguments passed to the script or function.

  • $#: The number of positional parameters.

  • $*: All positional parameters, as a single string.

  • $@: All positional parameters, as individual strings.

  1. What's The special parameter $?

The special parameter $? contains the exit status of the last executed command or script. An exit status of 0 indicates success, while a non-zero exit status indicates an error or failure. This parameter can be used in shell scripts to determine if a command or script was successful, and to take appropriate actions based on the result.

EXPANSIONS

What is the expansion and how to use them

Expansion:

Expansion refers to the process of replacing certain strings with their corresponding values. In shell scripting, there are several types of expansions that can be used:

  • Parameter expansion: This involves using variables and special parameters to expand their values.

  • Command substitution: This involves executing a command and substituting its output into the command line.

  • Arithmetic expansion: This involves performing arithmetic operations and expanding the result.

  • Brace expansion: This involves generating a series of strings based on a pattern.

    What is the difference between single and double quotes and how to use them properly

In shell scripting, single quotes ('') and double quotes ("") are used to enclose strings. The main difference between them is that strings enclosed in single quotes are treated as literals, while strings enclosed in double quotes are subject to parameter expansion and command substitution.

For example, if we have a variable named "myvar" with the value "hello world", we can use parameter expansion to print its value:

echo $myvar    # Output: hello world
echo "$myvar"  # Output: hello world
echo '$myvar'  # Output: $myvar

Note that in the last example, the single quotes prevent parameter expansion, so "$myvar" is treated as a literal string and not expanded.

How to do command substitution with $() and backticks?

Command substitution allows us to execute a command and use its output as a string in the command line. There are two ways to do command substitution in shell scripting:

  • Using the $() syntax:
echo "The date is: $(date)"
  • Using backticks (`):
echo "The date is: `date`"

Both of these examples will execute the "date" command and substitute its output into the string.

Here's an example of how to use command substitution to get a count of the number of files in the current directory:

file_count=$(ls | wc -l)
echo "There are $file_count files in this directory."

In this example, the "ls | wc -l" command is executed and its output (the number of files in the directory) is stored in the "file_count" variable. The variable is then used in a string to print the result.

Overall, understanding expansion, using single and double quotes properly, and knowing how to do command substitution are important skills for writing effective shell scripts.

SHELL ARITHMETIC

How to perform arithmetic operations with the shell

In shell scripting, we can perform arithmetic operations using arithmetic expansion, which involves enclosing the expression in double parentheses (( )) or using the expr command.

Here are some examples:

  1. Arithmetic expansion with double parentheses:
a=5
b=3
c=$((a + b))      # Add
d=$((a - b))      # Subtract
e=$((a * b))      # Multiply
f=$((a / b))      # Integer division
g=$((a % b))      # Modulo
h=$((a ** 2))     # Exponentiation

echo "c=$c d=$d e=$e f=$f g=$g h=$h"

Output: c=8 d=2 e=15 f=1 g=2 h=25

  1. Arithmetic expansion with expr:
a=5
b=3
c=$(expr $a + $b)      # Add
d=$(expr $a - $b)      # Subtract
e=$(expr $a \* $b)     # Multiply (note: * needs to be escaped with \)
f=$(expr $a / $b)      # Integer division
g=$(expr $a % $b)      # Modulo

echo "c=$c d=$d e=$e f=$f g=$g"

Output: c=8 d=2 e=15 f=1 g=2

Note that arithmetic expansion is more commonly used than expr, as it allows for more complex expressions and does not require escaping of certain characters. Additionally, arithmetic expansion can be used directly in variable assignments, while expr requires capturing the output with command substitution.

THE ALIAS COMMAND

How to create an alias

To create an alias in shell scripting, use the following syntax:

alias new_alias='command_to_alias'

For example, to create an alias for the "ls" command that always shows hidden files, you can use:

alias lsh='ls -a'

This creates a new alias called "lsh" that is equivalent to running "ls -a".

How to list aliases

To list all defined aliases, use the "alias" command without any arguments:

alias

This will print a list of all defined aliases in the current shell session.

How temporarily disable an alias

To temporarily disable an alias without removing it, you can use the "" escape character to bypass the alias and execute the underlying command directly.

For example, to run the "ls" command directly instead of using the "lsh" alias defined above, you can use:

\ls

This will run the "ls" command without the "-a" option that was defined in the "lsh" alias.

Alternatively, you can use the "unalias" command to remove an alias temporarily:

unalias alias_name

For example, to remove the "lsh" alias defined above, you can use:

unalias lsh

This will remove the "lsh" alias for the current shell session. However, the alias will still be defined in subsequent sessions unless it is removed permanently.

OTHER HELP PAGES

How to execute commands from a file in the current shell

To execute commands from a file in the current shell, you can use the "source" or "." command, which reads and executes commands from the specified file in the current shell environment.

Here's an example:

  1. Create a file called "myscript.sh" with the following contents:

     #!/bin/bas
     echo "Hello, World!"
    
  2. Make the file executable with the following command:

chmod +x myscript.sh
  1. Execute the script using the "source" or "." command:
source myscript.sh

or

. myscript.sh

Both of these commands will execute the script "myscript.sh" in the current shell environment, and the output "Hello, World!" will be printed to the console.

Note that using the "source" or "." command to execute a script is different from running it as a standalone executable. When executed as an executable, the script runs in a new shell environment, whereas using "source" or "." executes the script in the current shell environment. This can be useful for defining environment variables or aliases that should be available in the current shell session.

exercises

  1. Create an alias for the "ls" command that always shows file sizes in human-readable format (e.g. "ls -lh"). List all defined aliases to verify that the new alias has been created, and then temporarily disable the alias by running the "ls" command directly.
# Create alias for ls with human-readable file sizes
alias ls='ls -lh'

# List all defined aliases
alias

# Temporarily disable the alias
\ls

Exercise 2: Write a script that calculates the area of a rectangle based on user input for the length and width. Use arithmetic expansion to perform the calculation, and output the result to the console!

#!/bin/bash

# Read input from user for length and width
echo "Enter length:"
read length
echo "Enter width:"
read width

# Calculate area using arithmetic expansion
area=$((length * width))

# Output result to console
echo "The area of the rectangle is: $area"

Exercise 3:Create a shell variable called "myname" and assign it a value equal to your name. Use the "echo" command to output a greeting that includes your name using the variable.

#!/bin/bash

# Set shell variable myname
myname="Your Name"

# Output greeting using echo and variable
echo "Hello, my name is $myname"

Exercise 4: Write a shell script that takes a filename as input and counts the number of lines in the file using the "wc" command. Use command substitution to capture the output of the "wc" command and store it in a variable, and then output the variable to the console.

#!/bin/bash

# Read filename from user input
echo "Enter filename:"
read filename

# Count number of lines in file using wc and command substitution
num_lines=$(wc -l < "$filename")

# Output result to console
echo "The file $filename has $num_lines lines."

Exercise 5: Create a new shell function called "mycd" that takes a directory name as input and changes the current working directory to that directory. Use the "cd" command to change the directory, and then use the "pwd" command to verify that the directory has been changed. Call the function with a directory name to test it.

# Define mycd function
mycd() {
    # Change current working directory to input directory
    cd "$1"

    # Verify that directory has been changed using pwd
    echo "Current working directory: $(pwd)"
}

# Call function with directory name
mycd /path/to/directory

These exercises should help you practice the concepts covered in this tutorial. Don't hesitate to ask if you have any questions!

1. Create a shell variable called "mydir" and assign it a value equal to your current working directory. Use the "echo" command to output the value of the variable to the console.

  1. Write a shell script that takes two arguments as input: a filename and a search string. The script should use the "grep" command to search for the given string in the specified file, and output the matching lines to the console.

  2. Create an alias for the "cd" command that automatically runs the "ls" command after changing to a new directory. The alias should only work for the current shell session.

  3. Write a shell function called "countlines" that takes a filename as input and counts the number of lines in the file using the "wc" command. The function should return the line count as output.

  4. Write a shell script that prompts the user for a string input and stores it in a variable. The script should use the "sed" command to replace all occurrences of the letter "a" in the string with the letter "b", and output the modified string to the console.

I hope you found this tutorial on Shell, init files, variables, and expansions useful in learning more about the Shell environment and its various components. by now, you should have a solid understanding of how to work with variables, expansions, and init files, as well as some useful tips and tricks for improving your shell experience.

If you enjoyed this tutorial and want to learn more about tech and programming, I encourage you to follow me on other platforms where I share tips, tutorials, and other resources where I regularly post about a wide range of tech-related topics.

Linkedin

Twitter

Remember, the key to mastering any new skill is consistent practice and a willingness to learn. So keep experimenting, keep exploring, and most importantly, keep learning!