So far all the if else statement you saw were used in a proper bash script. That's the decent way of doing it but you are not obliged to it. When you just want to see the result in the shell itself, you may use the if else statements in a single line in bash. Suppose you hav...
Today, we studied the cat EOF command. We also gone through the working of cat EOF and the method to use it in the Bash script. After introducing it to you, we apply this with an example to make it easier to understand. We create the new Bash file, printed its data in the terminal...
In Bash, the term "shebang" refers to the first line of a Bash script that specifies which interpreter will be used when executing the script. The shebang line allows users to leverage the power of different interpreters or customize the script execution behavior. Bash scripts usually start wi...
This article is all about how to read files in bash scripts using awhile loop. Reading a file is a common operation in programming. You should be familiar with different methods and which method is more efficient to use. In bash, a single task can be achieved in many ways but there is...
bash let 1. Overview Implementing a counter is a common technique in almost any programming language. In this tutorial, we are going to see how to implement a counter in Bash script. Moreover, we’ll discuss some common pitfalls and how to solve the problems correctly. ...
Linux Sudo in Bash Script Examples If you’re an advanced Linux user, you must have faced situations where you want to log in as another user to run certain scripts. Well, the Sudo command helps users to run Linux commands at the root level of the system....
Exercise 1: Write a bash script that prints your username, present working directory, home directory and default shell in the following format. Hello, there My name is XYZ My current location is XYZ My home directory is XYZ My default shell is XYZ ...
There are two ways to parse arguments passed to the script in bash. One is writing the logic to parse the arguments manually using the special variables $@, $1, $2 … $N. Another way would be to usegetopts. Getopts is a POSIX compatible bash built-in function that accepts short argume...
$test3.sh#!/bin/bashPS4='LINENO:'set-xecho"hello World"mkdiir testing You can easily see the line number while reading the errors: $ /test3.sh5:echo'hello World'hello World6: mkdiir testing ./test3.sh: line6: mkdiir:commandnot found ...
Let me share a simple example of comparing two numberspassed as arguments to the shell script: #!/bin/bash ## Check if the numbers are equal or not read -p "Enter the first number: " num1 read -p "Enter the second number: " num2 ...