DEV Community

Cover image for Day-9: Bash Scripting - #1
FENIL SHAH
FENIL SHAH

Posted on

Day-9: Bash Scripting - #1

Day-9: Started my day with completing 5 exercises out of 35 of Unix Badge on pentesterlab! After that did some research on Bash scripting, follow my notes below to know more!

Bash Scripting, Whattttt?

  • Okay so, BASH stands for Bourne-Again SHell. what it does is that it takes in commands from the user and performs actions using operating system services. It is basically a shell interpreter!
  • A Bash script is a plain text file which contains a series of commands.
  • For example, the ls command lists the files and folders in a directory. Bash is the improved version of Sh (Bourne Shell).
  • Bash Scripting is used for:
    • Managing complexities and for automating recurrent tasks.

Writing the first Script!

  1. The first line should be "shebang" - (#!). Starts with hash character (#) and a bang character (!) and it declares the interpreter for the script.
    • #!/bin/bash - This line indicates that we are using bash interpreter!
  2. Adding Commands: The command “echo” prints out its arguments to standard output. “$@” is the variable name for all arguments passed in. Alt Text
  3. File Permissions: First save the file in your current directory with the extension of ".sh" (The “.sh” extension is conventional for shell scripts.) and then give permission with the following command! The “chmod” command edits the permissions for a file, and “+x” indicates that we want to add the permission to execute for all users.
chmod +x hello.sh
Enter fullscreen mode Exit fullscreen mode

4 . Executing the script: Now you can execute the file ./hello.sh and it will give you the following result!
Alt Text

PS: You can use any file editor like nano, vim, etc. Here I used nano hello.sh


Resources:

Bash Scripting Cheatsheet: https://devhints.io/bash
Bash Script examples: https://linuxhint.com/30_bash_script_examples/

Contact:

Got doubts? Contact me on Twitter.
Feedbacks are welcomed, do comment it down below! :)

Top comments (0)