Shell Scripting


title: Shell scripting

Shell Scripting

In the command line, a shell script is an executable file that contains a set of instructions that the shell will execute. Its main purpose is to reduce a set of instructions (or commands) to just one file. Also, it can handle logic because it’s also an interpreter.

How to Create a Shell Script

  1. Create the file:
$ touch myscript.sh

The file extension is not necessary. In linux, scripts can be executed even without .sh extension.

If the file is stored in /user/bin then the script should be able to be run from anywhere, provided the path is included in the $PATH variable.

  1. Add a shebang (#!) to the start of the file. The shebang line is responsible for letting the command interpreter know which interpreter the shell script will be run with.
$ echo "#!/bin/bash" > myscript.sh # or $ your-desired-editor myscript.sh # ---------- myscript.sh ------ #!/bin/bash ... # -----------------------------
  1. Add commands to the file:
$ echo "echo Hello World!" >> myscript.sh
  1. Give the file execution mode:
$ chmod +x myscript.sh
  1. Execute the script!
$ ./myscript.sh Hello World!

Additional Resources

This article needs improvement. You can help improve this article. You can also write similar articles and help the community.