In the realm of Command Line Interfaces(CLI), Bash symbols hold immense power. From manipulating variables to controlling flow and accessing command line arguments, these symbols unlock the true potential of the command line. In this article, I will explore the significance and applications of Bash symbols, equipping you with the knowledge to master their usage. Join me on this exciting journey into the world of Bash, where symbols become tools that revolutionize your command line experience.
List of Symbols Used in Bash Scripting
The most commonly used Bash symbols are listed below:
Symbol
Description
<
This is the input redirection symbol. For example, files can be used as input.
>
This is the output redirection operator. It is typically used to redirect the contents of a command or file to another file or command by overwriting it.
>>
This is a symbol to append any output to a file. It will not replace anything. Moreover, if the destination file is absent, this symbol will create the necessary file.
&>
It redirects both the standard output and the standard error.
2>&1
The symbol 2>&1 redirects both the standard output and the standard error to the standard output.
#
It is used for adding a comment line. These comments are not executed or run.
$#
The symbol $# is employed to fetch the length or count of arguments provided through the command line. When you use the symbol #@ or simply $1, $2, etc, it requests input from the command line and stores their values in a variable. The symbol $# is used to retrieve the overall number of arguments passed.
\< and \>
This is a symbol to compare length. If you need to compare the string length or character length, you can achieve your goal by using the symbols /< and />.
^^, ^ and ,,
If you need to change the case of the characters, you can do this by using the following symbols.
$@ or $*
The symbol $@ or $* refers to $1 $2 $3 $4 .. when you need to call all the arguments, just type $@ or $*. It will call all the arguments available.
$?
The $? symbol provides us with the exit status of the previously passed command. If the process was successfully done, then you will get a 0 exit status.
$$
The symbol $$ stores the current shells’ PID.
$!
The symbol $! refers to the process ID of the most recently executed command in the background.
$0
The $0 refers to the name of the shell or shell script. So you can use this if you want to print the name of the shell script.
$-
The symbol $- provides current option flags specified during the invocation, by the set built-in command or set by the bash shell itself.
$_
The symbol $_ refers to the absolute file name of the shell or bash script that is being executed as specified in the argument list.
6 Examples of Using Different Symbols in Bash Scripts
In this section, I will list some examples of bash scripts that include bash symbols. I hope this section will help to master your skill on Bash symbols and use them efficiently.
Example 01: Input Redirection Symbol in Bash Script
In this example, I will redirect a file named file1.txt to the cat command as input executing the below bash script which includes the <
symbol. To see practically, go through the following script:
#!/bin/bash
cat < file1.txt #file1.txt has been redirected as input of the cat command
The Bash script starts with the #!/bin/bash
line which specifies that it is a Bash script. The <
symbol has passed the file1.txt file as input of the cat command.
In the above image, you can see that upon execution the bashin.sh script has printed the contents of file1.txt after being redirected to the cat command as input.
Example 02: Output Redirection Symbol in Bash Script
In this example, I will redirect the output of the ncal command to the file2.txt file by running the below bash script which includes the >
symbol. Here, It will create the file2.txt file and keep the output of the ncal command in that file. The practical Bash script is as follows:
#!/bin/bash
ncal > file2.txt #Redirecting output of cal command to file2.txt with > symbol
cat file2.txt #Showing contents of the file2.txt
The >
symbol has passed the output of the ncal command to the file2.txt file. Then the cat command prints the contents of the file2.txt file.
In the preceding image, you can see that the command output has been redirected to the file2.txt file & printed on the terminal.
Example 03: Input Parameter Counter Symbol in Bash Script
Here, I will print the total number of arguments passed to a bash script by executing the following bash script which includes the $# symbol. See the below Bash script for the full process:
#!/bin/bash
echo "The Number of Parameter Passed In are: $#" #prints the total number of arguments passed
The echo command has printed the “The Number of Parameter Passed In are: $#” line where the $#
has fetched the total number of the parameters passed to the script and replaced itself with the number.
In the above image, you can see that the bash script has printed the total number of parameters passed to the script.
Example 04: Greater Than and Less Than Symbol
Here, I will now demonstrate to you how to compare two parameters by executing the following bash script, which includes the \<
and \>
symbols. Please go through the following script to see the process practically:
#!/bin/bash
a=$1; #setting the value of first parameter as a
b=$2; #setting the value of second parameter as b
if [ $a \< $b ]; #checks whether b is greater than a
then
echo "$b is Greater Input." #if b is greater than a then print thus
elif [ $a \> $b ]; #checks whether a is greater than b
then
echo "$a is Greater Input." #if a is greater than b then print thus
else
echo "The Two Inputs Are Equal." #else the inputs are equal and this will be printed
fi
At first, a
and b
take the first and second input parameters respectively. Then the if condition has checked whether b is greater than a. If so, then it will print that “b is Greater Input” Here b will be replaced by the original value. Otherwise, the elif block will check whether a is greater than b. If so, it will print that “a is Greater Input”. Here a will be replaced by the original value. If neither is correct. It will print that “The Two Inputs Are Equal”.
The above image shows that the bash script has successfully compared the two input parameters.
Example 05: Case Changing Symbol
In this example, I will develop a bash script that will change the case of my input. To do so, execute the following script:
!/bin/bash
a=$1;#a is set as the output is
echo "Your text in upper case:${a^^}"#all letter are upper_case
echo "Your text in lower case:${a,,}"#all letter are lower_case
echo "Your text where first letter is upper case:${a^}"#only first letter is upper
At first, the a
has taken the value of the first input parameter. Then the first echo command printed the intended text where all letters are in upper case with the help of the a^^
symbol, the second echo command printed the intended text where all letters are in lower case with the help of the a,,
symbol, and the last echo command has printed the intended text where only the first letter is the upper case with the help of the a^
symbol.
The prior image shows that the Bash script has changed the case of my input.
Example 06: Taking All Parameters Symbol
In this example, I will develop a bash script that will include the $@
symbol which takes all the input parameters in it. Here I will print all the input parameters using this symbol. To do so, follow the below script:
#!/bin/bash
for i in $@ #take all input parameters in i one by one
do
echo -e "$i\n" #prints the value of i in a single line
done
for i in $@
initiates a for loop and takes the first parameter from all the parameters that are passed to the scripts and set as i
then execute the echo -e "$i\n"
command which prints the variable on the terminal. Here -e
enables the interpretation of backslash escapes, allowing the newline character to be recognized. After executing this, the next parameter is set as i
, and the same task is executed till the last parameter. The done
marks the end of the for loop.
The above image shows that the allpara.sh bash script has printed all the input parameters with the help of the $@ symbol.
Conclusion
Learning about the most commonly used bash symbol is essential for efficient bash scripting. Furthermore, these symbols provide the foundation for creating powerful and robust scripts. By understanding and utilizing these symbols effectively, programmers can enhance script readability, maintainability, and performance, enabling them to automate tasks and streamline their workflow.
People Also Ask
What does $@ mean in bash?
In Bash, $@
is a special variable that represents all the positional parameters passes to a script or function. It expands to the list of arguments provided to the script, each treated as a separate word.
How do I compare symbols in bash?
By using the if statement you can check the equality and inequality of two strings. You need to put ==
to check equality and !=
to check the inequality of the strings.
Why use brackets in bash?
When it is time to compare variables in Bash, programmers generally use single brackets ([ ]) and double brackets ([[ ]]).
What are the data types in Bash?
There are no certain data types in Bash. A variable in bash can contain a number, a character, or a string of characters. Programmers need not declare a variable, just assigning a value to its reference will create it.
How do I chain a command in bash?
You can use the ampersand operator (&) to run commands in the background. When you append & at the end of any command, the command will be executed in the background. Moreover, you can even use & in the middle of the multiple commands to run multiple commands in the background.
Related Articles
- How Will You Start Writing Bash Scripts [A Complete Overview]
- An Ultimate Guide to Bin Bash Script and Shebang
- Most Used Bash Script Commands [The Ultimate Review]
- 8 Practical Examples of Chaining Commands in Bash Scripts
<< Go Back to Writing Bash Script | Bash Scripting Basics | Bash Scripting Tutorial
FUNDAMENTALS A Complete Guide for Beginners