Many built-in commands exist in Bash to perform different types of tasks. Bash has no built-in function like other programming languages. But the tasks of the built-in functions can be done easily using the different types of built-in commands of Bash. Bash has some special built-in commands and some inherited built-in commands which are inherited from the shell commands. The purposes of the most commonly used built-in commands of Bash and the uses of some built-in commands are described in this tutorial.
List of Some Useful Built-In Commands
Command | Purpose |
echo | To write the string data in the output. |
printf | To write the formatted string data in the output. |
read | To take an input from the user. |
pwd | To print the absolute path of the current working directory. |
date | To print the current date and time of the system. |
declare | To declare a variable with the data type. |
set | To modify the behavior of the current shell. |
unset | To remove any variable and function names. |
ls | To print the list of all files and folders of the current location. |
cat | To create or read a file. |
rm | To remove one or more files. |
mkdir | To create a new directory. |
rmdir | To remove any directory. |
dir | To print the directory stack of the shell. |
cd | To change the current directory location. |
let | To perform the arithmetic operations. |
alias | To create a shortcut of any command. |
unalias | To delete any previously created alias command. |
return | To return the value from the function. |
eval | To evaluate any expression as the command. |
history | To print the history information. |
export | To pass the shell variable or function from the parent to the child process. |
help | To print the help information about built-in shell commands. |
exit | To exit to the terminal from the shell with exit status code. |
Examples of Some Bash Built-In Commands
The uses of some useful Bash built-in commands are shown in this part of the tutorial.
Example 1: “Ls” Command
Run the following command to print the list of the current files and folders:
Run the following command to print the list of all files with the “bash” extension:
Run the following command to print the list of all files and folders that starts with the “error”:
Output:
The following output appears after executing the previous commands:
Example 2: “Cd” Command
Run the following command to change the current directory location to “/home/fahmida/code”:
Run the following command to check the content of the changed directory:
Output:
The following output appears after executing the previous commands:
Example 3: “Cat” Command
Run the following command to create a new file named languages.txt and add some content to the file. Press Ctrl+D to go to the command prompt after adding the content.
Run the following command to print the content of the file:
Run the following command to append the content to the file:
Run the following command to print the content of the file after appending:
Output:
The following output appears after executing the previous commands:
Example 4: “Echo” Command
Run the following command to print a simple string:
Run the following command to print the string with a newline:
Output:
The following output appears after executing the previous commands:
Example 5: “Read” Command
Run the following command to take the input in the $name variable:
Run the following command to print the input value:
Run the following command to take the input with the prompt message:
Run the following command to print the input value:
Output:
The following output appears after executing the previous commands:
Example 6: “Mkdir” Command
Run the following command to create a directory named “temp”:
Run the following command to check whether the directory is created or not:
Output:
The following output appears after executing the previous commands:
Example 7: “Date” Command
Run the following command to print the current date and time of the system:
Output:
The following output appears after executing the previous command:
Example 8: “Declare” Command
Run the following command to declare a variable named $number of integer type:
Run the following command to assign the string value to the $number variable that can take only the integer value:
Run the following command to print the current value of the $number variable which is 0 to assign the string value to the integer variable:
Run the following command to assign the number value to the $number:
Run the following command to print the current value of the $number variable:
Output:
The following output appears after executing the previous commands. The output shows that the number variable contains 0 when the string value is assigned to a variable to declare the variable by the “declare” command:
Example 9: “Eval” Command
Run the following command to assign a command to the variable:
Run the following command to assign a string value to a variable:
Run the following command to execute the “echo” command using the “eval” command:
Output:
The following output appears after executing the previous commands:
Example 10: “History” Command
Run the following command to print the history information of the current terminal:
Output:
The following output appears after executing the previous commands. The output shows that four commands are executed after opening the terminal:
Conclusion
The most commonly used Bash built-in functions are described in this tutorial using different types of examples to help the new Bash users to work in the Linux environment.