Jan 26, 2026
Ariffud M.
4min Read
If you’ve ever utilized a Linux operating system, you whitethorn person heard of bash. It’s a Unix ammunition that sounds and executes various commands.
When you request to tally respective bash commands, you don’t person to execute them manually 1 astatine a time. Instead, it’s imaginable to create a book record that contains bash functions to tally those commands.
It whitethorn sound complicated, but by learning its basics, you will understand nan bash scripting connection and find retired really it tin thief your workflow.
This article will screen nan process of bash scripting. We’ll spell complete everything from bash commands to moving a bash programme connected a Linux terminal.
What is bash?
Bash, short for Bourne-Again Shell, is simply a Unix ammunition and a bid connection interpreter. It sounds ammunition commands and interacts pinch nan operating strategy to execute them.
To afloat understand bash ammunition scripting, you request to cognize 2 concepts – shell and scripting.
Shell is simply a macro processor that uses commands to interact pinch nan operating system. This intends that it tin retrieve, process, and shop accusation connected a computer.
Meanwhile, scripting is nan process of compiling ammunition commands into a caller record utilizing a matter editor.
When you constitute bash successful a matter editor, you’re compiling bash commands or bash functions – a group of commands that tin beryllium called galore times by only utilizing nan usability name. The matter is past saved arsenic an executable bash script record pinch nan .sh extension.
Why should you usage a bash script?
Bash scripts tin thief pinch your workflow arsenic they compile galore lengthy commands into a azygous executable book file.
For example, if you person aggregate commands that you person to tally astatine a circumstantial clip interval, you tin compile a bash book alternatively of typing and executing nan commands manually 1 by one. You only request to execute nan book record erstwhile it’s necessary.
Here are immoderate different advantages of utilizing bash scripts:
- Well-structured commands – building nan commands successful a series truthful that each clip you execute nan script, it will tally successful nan correct order.
- Task automation – automate nan book execution astatine immoderate defined clip utilizing cron’s time-based scheduler.
- Transparency – group tin cheque nan contented of a book since it’s successful a readable matter file. However, if you tally nan commands utilizing different programme written successful a different programming language, specified arsenic C++, you’ll request to entree nan root code.
- Transferable – if you transportation a book to different Linux distributions, it’ll still work, providing that ammunition commands are disposable connected that peculiar operating system.
Understanding bash commands
Bash is disposable connected almost each types of Unix-based operating systems and doesn’t require a abstracted installation. You will request a Linux command line, besides known arsenic nan Linux terminal. It’s a programme that contains nan ammunition and lets you execute bash scripts.
Use this bid to cheque nan database of disposable shells connected your Unix operating system:
cat /etc/shellsThe output should show a database for illustration this:
/bin/bash /bin/sh /bin/tcsh /bin/cshEach bash ammunition book needs to commencement pinch #! followed by nan absolute way to nan bash interpreter. To position nan path, participate this command:
which bashIt should nutrient nan pursuing output:
/bin/bashThis is nan modular way to nan bash expert connected astir Unix operating systems. To fto nan ammunition cognize that it should tally commands utilizing nan bash interpreter, commencement nan book pinch this line:
#!/bin/bash
[pro tip]Linux has a bash ammunition bid manual. It contains descriptions of each method position and modular ammunition variables. Type and execute nan man bash bid to show nan manual connected nan terminal.[/protip]
The adjacent measurement is to constitute and compile nan commands successful a .sh record utilizing a matter editor. You will request a Unix matter editor specified arsenic VIM aliases GNU Nano. In this tutorial, we’ll usage nan Nano text editor to create nan record by inputting this command:
nano function.shThis will unfastened a caller .sh record for you to edit. Begin by penning #!/bin/bash followed by bash commands.
Once you’re done utilizing nan Nano matter editor, property CTRL+X to adjacent it, past property Y and Enter to prevention nan changes.
In bid to successfully create your first bash script, you request to understand nan basal bash commands. They are nan main elements of a script, and you must cognize what they do and really to constitute them properly.
Read our bash commands and book examples to thief you get started.
How to tally a bash script
Now that we person written a bash script, let’s study really to tally it from nan terminal. There are 3 methods to do it – utilizing nan bash command, utilizing nan ./ command, and moving nan book from a different directory.
Using nan bash command
The first method is by utilizing nan bash bid from nan due directory. For example, you whitethorn person a function.sh bash book containing elemental echo functions successful nan Test/Bash directory. You person to unfastened nan directory first by utilizing this command:
cd Test/BashThen, execute nan pursuing bash command to tally nan bash script:
bash function.shYou should spot nan output for illustration this:

If you effort to tally nan book without nan bash command, you’ll person a command not recovered error message.

Using nan ./ command
You tin tally a bash book without nan bash command. However, you person to group nan record to person nan execute permission utilizing nan pursuing bid from nan due directory:
chmod +x function.shThis bid modifies nan record permissions truthful that everyone tin execute nan file. Once you’ve done that, execute nan bash book by utilizing this command:
./function.shIf you don’t group nan support correctly, nan terminal will people a Permission denied error message:

Like nan bash command, you’ll besides get a command not recovered error if you don’t usage ./ in your command.
Run nan book from a different directory
Another replacement is to tally nan book from a different directory. After you create nan bash script, usage nan pwd command to find your existent directory. Once you cognize nan path, you tin tally nan book from immoderate directory. For example, usage nan pursuing bid to tally function.sh from nan location directory:
bash Test/Bash/function.shUse nan cd bid to spell to nan location directory consecutive distant sloppy of nan directory you are in.
Next step: Practicing and mastering bash scripting
Bash sounds ammunition commands and interacts pinch nan operating strategy to execute them. The awesome facet of bash is that you tin compile galore bash commands and functions into a azygous executable script, helping you streamline your workflow.
However, location is overmuch much to study if you want to beryllium capable to utilize nan afloat imaginable of bash. Practice pinch nan examples we person provided and proceed exploring bash truthful you tin constitute amended and much businesslike scripts. We urge proceed learning astir bash variables arsenic they tin make your book much flexible. Good luck!
All of nan tutorial contented connected this website is taxable to Hostinger's rigorous editorial standards and values.
3 hari yang lalu
English (US) ·
Indonesian (ID) ·