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.
Key Takeaways
- Getting familiar with the most used symbol for Bash script.
- Learning the usage of symbols in bash scripting.
Free Downloads
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 which is being executed as specified in the argument list.
6 Examples Related to Using 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.
Script (bashin.sh) >
#!/bin/bash
cat < file1.txt #file1.txt has been redirected as input of the cat command
In the above image, you can see that the bashin.sh has printed the contents of the file1.txt file as the file1.txt file is 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.
Script (bashout.sh) >
#!/bin/bash
ncal > file2.txt #Redirecting output of cal command to file2.txt with > symbol
cat file2.txt #Showing contents of the file2.txt
In the preceding image, you can see that the output of the ncal command has been redirected to the file2.txt file. Then the cat command has printed the contents of the file2.txt file 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.
Script (bashparanum.sh) >
#!/bin/bash
echo "The Number of Parameter Passed In are: $#" #prints the total number of arguments passed
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.
Script (bashcomp.sh) >
#!/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 b
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
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.
Script (case.sh) >
!/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
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.
Script (allpara.sh) >
#!/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
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
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