This post covers basic tutorials on Bash commands and programming with examples.
The course covers Bash and shell programming features, including variables, loops, conditional expressions, and operators.
Shell is a command-line interpreters, It is an application to provide commands to the different operating systems such as Linux, Unix, and Mac.
Bash is an enhanced version of shell, an layer between user and operating system function calls.
User types the commands via command line or batch of commands also called scripts.
Requirement
To follow this course, you should have a basic understanding of computer language, and the following are required:
- Linux-flavored operating system
- Basic knowledge of any programming language
What is Bash
Bash, short for Bourne Again Shell, is an open-source command-line shell interpreter and scripting language, that servces as a interface between user and Operationg System.
It functions both as a interactive shell command for executing commands in real time, and as a scriting programming language for running script files.
Thank of bash as your system translator: It takes the commands you type and translates them into actions your computer can understand and execute.
You can use Bash in two main ways:
- Interactive mode: Also called a command interpreter, it allows the execution of commands in the terminal. It executes commands sequentially if there are multiple commands.
- Script : This refers to non interactive mode, allowing you to write Bash syntax that contains a sequence of multiple commands, executes the multiple commands one after another in a single file.
Key Characterstics:
- Interactive: Input commands in the terminal, executes the commands in real time.
- Scripting: Write multiple commands or programs in a file, executes and automates the tasks.
- Cross Platform: It works on all platforms like Linux, MacOS, Windows and Unix OS.
- Backward compatable: Latest versions of bash supports older versions of commands and scripts.
What is the difference between Bash and shell
Shell, an alias for Bourne Shell, is a command-line interpreter for Unix and Linux OS. Bash, an alias for Bourne Again Shell, is an enhanced version.
What is bash scripting used for?
Bash scripting has multiple use cases, including:
- Automate tasks
- Writing scripts to automate programming tasks
- Synchronizing tasks to copy files
- Executing cron jobs for scheduling
How do I write code in bash?
To write code in a Bash script, follow these steps:
- In the terminal, create a file using
vi test.sh
. - Add
#!/bin/bash
at the top of the file. - Add multiple shell code snippets.
- Save the shell file with a
.sh
extension. - Execute the shell script using the
./test.sh
command in the terminal.
Is bash coding language?
Bash executes commands from the terminal or a file. It is a programming language that operates in Unix/Linux kernel operating systems, containing all the features to write complete code.
Bash is a special type of shell that takes input from commands, runs the code, and processes the input, returning results.
Shell Types
There are different types of shells in a Unix OS.
Shell Type | Alias | First Line |
---|---|---|
sh | Bourne Shell | #!/bin/sh |
bash | Bourne Again shell | #!/bin/bash |
cshell | C shel | #!/bin/csh |
| tcsh | TENEX C shell | #!/bin/tcsh | | | | ksh | Korn shell | #!/bin/ksh |
Difference between Command Line and Script in bash
Let’s see the difference between command line and scripts
Commands line options
- Command line has a prompt which takes input from a user
- Commands are not saved to a file.
- It supports only one command at a time.
Script files
- Support multiple commands in a single file
- Prompt still can be written in the script file
- Only one line in a file executes sequentially