DEV Community

loading...
Cover image for Day-9: Bash Scripting - #1

Day-9: Bash Scripting - #1

FENIL SHAH
19 | Security Enthusiast | GitHub Campus Expert 🚩 | Mozilla Campus Club Lead | THM | HTB
・2 min read

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! :)

Discussion (0)

Forem Open with the Forem app