Skip to content

Click on each book below to review & buy on Amazon.

As an Amazon Associate, I earn from qualifying purchases.


RHCSA - Create Simple Shell Scripts

A shell script is a computer program that contains a series of commands written in a shell scripting language, such as Bash (Bourne Again SHell). It is designed to be executed by a shell interpreter. Shell scripts provide a convenient and powerful way to automate tasks and execute complex operations by combining multiple commands into a single script.

Shell scripts can perform a wide range of functions, from simple tasks like file manipulation and system administration to more complex operations like process management and network programming. They can interact with the operating system, execute system commands, manipulate files and directories, control program flow with conditionals and loops, and handle user input and output.

One of the primary advantages of shell scripting is its flexibility and portability across different Unix-like systems. Shell scripts are interpreted rather than compiled, allowing for easy modification and quick testing. They can also be used as a scripting language for system administration, automating repetitive tasks, and creating customized solutions.

Interpreters

There are several shell interpreters available that can execute shell scripts. Here are some commonly used ones:

  • bash (Bourne Again SHell): Bash is one of the most popular and widely used shell interpreters. It is the default shell on most Linux distributions.
  • sh (Bourne Shell): Bourne Shell was one of the original Unix shells and serves as the basis for many other shells. It is available on most Unix-like systems.
  • csh (C Shell): C Shell is a shell with a syntax resembling the C programming language. It offers additional features like command-line editing and history. It is commonly found on BSD-based systems.
  • ksh (Korn Shell): Korn Shell: The Korn Shell (ksh) is a powerful and versatile shell that incorporates features from the Bourne shell (sh) and the C shell (csh). It offers advanced scripting capabilities, command-line editing, and programmable command-line completion.
  • tcsh (TENEX C Shell): TENEX C Shell is an extended version of C Shell that incorporates additional features and improvements. It is known for its interactive command-line editing capabilities.
  • zsh (Z Shell): Zsh is an extended shell that includes features from various shells, including Bash, Korn Shell, and C shell. It provides advanced command-line editing, powerful scripting capabilities, and extensive customization options.

These are just a few examples of shell interpreters, and there are others available as well. The choice of shell interpreter depends on the specific requirements and preferences of the user or system administrator.

shebang / shabang / hashbang

The shebang tells the system which interpreter should be used to execute the script. In the case of a bash script, the shebang is typically written as:

#!/bin/bash

Let's break down the shebang line:

  • The first two characters, #!, indicate to the system that this is a shebang line.
  • Following the shebang indicator is the path to the interpreter, in this case, /bin/bash. It specifies the location of the Bash interpreter executable on the system.
  • After the interpreter's path, any additional arguments or options can be provided. For example, #!/bin/bash -e would start the script with the -e option, which causes the script to exit immediately if any command within it fails.

When you run a script with a shebang line, the system looks at the specified interpreter, such as /bin/bash, and passes the script file to that interpreter for execution. This allows you to write scripts in different programming languages and have them automatically executed by the appropriate interpreter.

The shebang line must be the very first line in the script file and should not be preceded by any whitespace or comments. However, the shebang line is optional. If a script does not have a shebang line, it can still be executed by explicitly passing it to the appropriate interpreter (e.g., bash script.sh).

Using the shebang line allows you to make your scripts more portable and executable with a simple command, such as ./script.sh, instead of explicitly specifying the interpreter every time.

Create and Run a Simple Shell Script

To create and run a shell script you first need to create a file with the appropriate extension, such as .sh. This is not required in Linux as file extensions do not mean anything, however it makes readability for everyone that much better.

After creating the file, it will need the interpreter and commands adding that you want to run, with the file being made executable.

Creating & running a simple shell script:

# Create a suitable directory for the script
mkdir -p ~/scripts

# Change into scripts directory
cd scripts

# Create script to perform simple calculation
cat << 'EOF' > calc.sh
#!/bin/bash

# This is a basic shell script demo
# It performs a simple calculation and displays the result

num1=3
num2=5

result=$(( num1 + num2 ))

echo "The result of the calculation is: $result"

EOF

# Give the script execute permissions
chmod -v u+x calc.sh

# Execute script
./calc.sh 

Support DTV Linux

Click on each book below to review & buy on Amazon. As an Amazon Associate, I earn from qualifying purchases.

NordVPN ®: Elevate your online privacy and security. Grab our Special Offer to safeguard your data on public Wi-Fi and secure your devices. I may earn a commission on purchases made through this link.