Creating and Running Shell Scripts: Format and Execution

Linux+ · Automation, Orchestration, and Scripting · By Super Admin

Learn the proper format for creating shell scripts, including shebang lines and comments, and how to execute them with proper permissions.

Shell Script Structure and Format Requirements

Shell scripts are plain-text files that contain multiple commands for the Linux shell to run. To create a shell script file, you can use any text editor that you're comfortable with, depending on your working environment. If you're working from a KDE-based graphical desktop, you can use the KWrite program, or if you're working from a GNOME-based graphical desktop, you can use the GEdit program. For those working directly in a command-line environment, many Linux distributions include either the pico or the nano editor to provide a graphical editor environment by using ASCII control characters to create a full-screen editing window. If your Linux distribution doesn't include either of these editors, the vi editor remains available as a last resort, though it is a text-based editor that uses simple single-letter commands and dates back to the early days of Unix.

Once you've chosen your text editor, you must follow a specific format for the shell script file to ensure it works correctly. The first line in the file usually specifies the Linux shell required to run the script, written in a specific format using the shebang. The Linux world calls the combination of the pound sign and the exclamation symbol (#!) the shebang, and it signals to the operating system which shell to use to run the shell script. Most Linux distributions support multiple Linux shells, but the most common is the Bash shell. You can run shell scripts written for other shells as long as those shells are installed on the Linux distribution. If you don't specify the shebang line in your shell script, Linux will run the script using the default shell defined for your user account in the /etc/passwd file, though it's usually recommended to specify a shell, even if it is your default shell, to avoid any confusion or mistakes.

Comments and Command Organization

After you specify the shell with the shebang line, you're ready to start listing the commands in your script. Unlike entering commands at the command prompt where you must use semicolons to separate multiple commands on a single line, Linux allows you to place commands on separate lines within a shell script. The Linux shell assumes each line is a new command in the shell script, so you don't need to use semicolons to separate the commands. This makes shell scripts much more readable and easier to maintain than trying to fit everything on a single command line.

Lines that start with a pound sign are called comment lines, and they allow you to embed comments into the shell script program to help you remember what the code is doing. The shell skips comment lines when processing the shell script, so they have no effect on the script's execution. You can place comment lines anywhere in your shell script file after the opening shebang line. Using the .sh filename extension on the shell script file, although not required in Linux, has become somewhat of a de facto standard among programmers because it helps identify that the text file is a shell script that can be run at the command line.

Executing Shell Scripts and Managing Permissions

When you attempt to run a shell script file by simply entering its name at the command prompt, you may encounter an error because the shell doesn't know where to find the file. The shell uses a special environment variable called PATH to list directories where it looks for commands. If your local HOME folder is not included in the PATH environment variable list of directories, you can't run the shell script file directly. Instead, you need to use either a relative or an absolute path name to point to the shell script file, with the easiest way being to add the ./ relative path shortcut to the file.

Even after specifying the correct path, you may encounter a permission error indicating that you don't have permissions to run the shell script file. By default, the Linux system doesn't give anyone execute permissions to run the file. You can use the chmod command to add that permission for the file owner by using the u+x option, which adds execute privileges to the owner of the file. Once you've added execute permissions using chmod, you should be able to run the shell script file and see the output from the commands it contains.

0.0 (0 ratings)

Discussion (0)

No discussion on this article yet — be the first to comment.

Please sign in to join the discussion.