| by Arround The Web | No comments

Bash Built-In Commands

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:

$ ls

 
Run the following command to print the list of all files with the “bash” extension:

$ ls *.bash

 
Run the following command to print the list of all files and folders that starts with the “error”:

$ ls 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”:

$ cd /home/fahmida/code

 
Run the following command to check the content of the changed directory:

$ ls

 
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.

$ cat > languages.txt

 
Run the following command to print the content of the file:

$ cat languages.txt

 
Run the following command to append the content to the file:

$ cat >> languages.txt

 
Run the following command to print the content of the file after appending:

$ cat languages.txt

 
Output:

The following output appears after executing the previous commands:


Example 4: “Echo” Command

Run the following command to print a simple string:

$ echo "Welcome to LinuxHint"

 
Run the following command to print the string with a newline:

$ echo -e "Welcome\nTo\nLinuxHint"

 
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:

$ read name

 
Run the following command to print the input value:

$ echo $name

 
Run the following command to take the input with the prompt message:

$ read -p 'Enter username:' un

 
Run the following command to print the input value:

$ echo $un

 
Output:

The following output appears after executing the previous commands:


Example 6: “Mkdir” Command

Run the following command to create a directory named “temp”:

$ mkdir temp

 
Run the following command to check whether the directory is created or not:

$ ls

 
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:

$ date

 
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:

$ declare -i number

 
Run the following command to assign the string value to the $number variable that can take only the integer value:

$ number='abc'

 
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:

$ echo $number

 
Run the following command to assign the number value to the $number:

$ number=20

 
Run the following command to print the current value of the $number variable:

$ echo $number

 
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:

$ cmd="echo"

 
Run the following command to assign a string value to a variable:

$ str="Hello World"

 
Run the following command to execute the “echo” command using the “eval” command:

$ eval $cmd $str

 
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:

$ history

 
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.

Share Button

Source: linuxhint.com

Leave a Reply