Linux shell script collection


Q: 1 what is a shell script and is it required?

A: a shell script is a text file that contains one or more commands. As a system administrator, we often need to use multiple commands to complete a task. We can add all these commands in a text file (shell script) to complete these daily tasks.

Q: 2 what is the default login shell and how to change the login shell of the specified user

A: on Linux operating system, “/ bin / bash” is the default login shell, which is allocated when creating users. Use the Chsh command to change the default shell. Examples are as follows:

# chsh <username> -s <new_default_shell>
# chsh linuxtechi -s /bin/sh

Q: 3 what types of variables can be used in shell scripts?

A: in the shell script, we can use two types of variables:

  • System defined variable
  • User defined variable

System variables are created by the system itself. These variables are usually composed of uppercase letters and can be“set”Command view.

User variables are generated and defined by system users, and the values of variables can be defined by commands“Echo $< variable name >”View.

Q: 4 how to redirect standard output and error output to the same location at the same time?

A: there are two ways to achieve this:

Method 1:

2>&1 (# ls /usr/share/doc > out.txt 2>&1 )

Method 2:

&> (# ls /usr/share/doc &> out.txt )

Q: 5 how to nest “if” syntax in shell script?

A: the basic grammar is as follows:

if [ Condition ]
if [ condition ]

Q: 6. What is the purpose of “$?” tag in shell script?

A: when writing a shell script, if you want to check whether the previous command is executed successfully, use “$?” in the if condition to check the end state of the previous command. Simple examples are as follows:

[email protected]:~# ls /usr/bin/shar
[email protected]:~# echo $?

If the end status is 0, the previous command is executed successfully.

[email protected]:~# ls /usr/bin/share
ls: cannot access /usr/bin/share: No such file or directory
[email protected]:~# echo $?

If the end state is not 0, the command execution fails.

Q: 7 how do I compare two numbers in a shell script?

A: use the test command (- GT, etc.) in if then to compare two numbers. Examples are as follows:

if [ $x -gt $y ]
echo “x is greater than y”
echo “y is greater than x”

Q: 8 what is the role of the break command in shell scripts?

A: a simple use of the break command is to exit a loop in execution. We can use the break command in the while and until loops to jump out of the loop.

Q: What is the function of continue command in 9 shell script?

A: the continue command is different from the break command. It only jumps out of the iteration of the current loop, notwholeCycle. The continue command is often useful, such as when an error occurs, but we still want to continue to execute a large loop.

Q: 10 tell me the syntax of the case statement in the shell script?

A: the basic grammar is as follows:

case word in

Q: 11 while loop syntax in shell script?

A: like the for loop, the while loop repeats its command block as long as the condition is true. Unlike the for loop, the while loop iterates until its condition is not true. Basic syntax:

while [ test_condition ]

Q: 12 how do I make scripts executable?

A: use the Chmod command to make the script executable. Examples are as follows:

# chmod a+x

Q: 13 “#! / bin / bash”?

Answer: #/ Bin / Bash is the first line of the shell script, called the shebang line. The # symbol here is called hash, and! It’s called bang. It means that the command is executed through / bin / bash.

Q: 14. For loop syntax in shell script?

Answer: the basic syntax of the for loop:

for variables in list_of_items

Q: 15 how to debug shell scripts?

A: shell scripts can be debugged using the ‘- x’ parameter (SH – x myscript. SH). Another way is to use the ‘- NV’ parameter (SH – NV myscript. SH).

Q: How do 16 shell scripts compare strings?

A: the test command can be used to compare strings. The test command compares by comparing each character in the string.

Q: 17 what are the special variables in Bourne shell (bash)?

A: the following table lists the special variables set by the Bourne shell for the command line.

Built in variable



Script name in command line


First command line parameter


Second command line argument




Ninth command line parameter


Number of command line arguments


All command line arguments, separated by spaces

Q:18 How to test files in a shell script ?

Q: 18 how do I test files in a shell script?

A: the test command can be used to test files. The basic usage is shown in the following table:



-D file name

Returns true if the file exists and is a directory

-E file name

Returns true if the file exists

-F file name

Returns true if the file exists and is a normal file

-R file name

Returns true if the file exists and is readable

-S file name

Returns true if the file exists and is not empty

-W file name

Returns true if the file exists and is writable

-X file name

Returns true if the file exists and is executable

Q: 19 how do I write comments in a shell script?

A: comments can be used to describe what a script can do and how it works. Each line of comment begins with #. Examples are as follows:

# This is a command
echo “I am logged in as $USER”

Q: How to make the shell get input from the terminal on the script?

A: the read command can read data from the terminal (using the keyboard). The read command takes the user’s input and places it in the variable you give. Examples are as follows:

# vi /tmp/
echo ‘Please enter your name'
read name
echo “My Name is $name”
# ./
Please enter your name
My Name is LinuxTechi

Q: 21 how to cancel a variable or its assignment?

A: the “unset” command is used to cancel the variable or the variable assignment. The syntax is as follows:

# unset <Name_of_Variable>

Q: 22 how to perform arithmetic operations?

A: there are two ways to perform arithmetic operations:

useexprCommand (# expr 5 + 2) 2. Use a dollar sign and square brackets($[expression])For example: test = $[16 + 4]; test=$[16 + 4]

Q: 23 basic format of do while statement?

A: the do while statement is similar to the while statement, but execute the command before checking the conditional statement. The following is the syntax for using the do while statement

} while (condition)

Q: 24 how to define functions in shell scripts?

A: a function is a code block with a name. When we define a code block, we can call the function name in our script, and the block will be executed. Examples are as follows:

$ diskusage () { df -h ; }

Q: 25 how to use BC (bash calculator) in shell script?

Answer: use BC in the shell script using the following format:

variable=`echo “options; expression” | bc`

Original source:  linuxtechi    Source of Translation:LCTT