Unix
15 MarchCheck if String Starts with Another String in Bash
Using Regex Operator with if statement To check if string starts with another string in bash, use the regex operator =~ with if statement. string="hello world" prefix="hello" if [[ "$string" =~ ^"$prefix" ]]; then echo "The string starts with the prefix." else echo "The string does not start with the prefix." fi The string starts […]
13 MarchGet Random Number Between 1 and 100 in Bash
Using $RANDOM Variable Use the $RANDOM variable to generate a random number between 1 and 100 in Bash. echo $((1 + $RANDOM % 100)) 79 The above code generates a random integer number between 1 to 100 and prints it on the bash console. How? The $RANDOM generated a random integer between 0 and 32767. […]
11 MarchCheck Number of Arguments in Bash
1. Overview In Bash scripting, it’s often necessary to verify the number of arguments passed to a script to ensure correct script execution. This is crucial in scenarios where the script’s behavior depends on user-provided inputs. In this article, we will see different ways to check number of arguments in bash. 2. Introduction to Problem […]
07 MarchCheck if Command Exists in Bash
Use command Command Use command to check if the specified bash command exists. if command -v curl > /dev/null; then echo "curl command exists." else echo "curl command does not exist." fi curl command exist The command command is used to run a command bypassing any aliases or functions that may be defined. We can […]
07 MarchGet Filename without Extension in Bash
TL;DR If you know extension of the file, you can use basename command and pass filename and extension as parameters to retrieve filename without extension in bash. This is useful when you have file extension like .tar.gz filename=/home/john/Desktop/test.tar.gz basename ${filename} .tar.gz test Using basename Command Use the basename command to get the filename without extension […]
07 MarchCheck if Array Contains Value in Bash
Using echo with grep Command To check if a Bash array contains a value, use the echo command and pipe it to grep command to search the value from the defined array. #!/bin/bash #declare an array my_array=("apple" "banana" "cherry" "date") #value to search for search_value="banana" #check if the array contains the value with grep if […]
28 FebruaryCheck If Variable Is Empty in Bash
Checking If Variable Is Empty in Bash There are multiple ways to check if a variable is empty or not. Before moving towards those methods, knowing when a variable is called empty is necessary. In Bash, a variable is called empty if: A variable is declared without assigning any value to it. A variable is […]
19 FebruaryWhat is echo $1 in Bash
Meaning of the echo $1 in Bash The echo $1 is a bash command; now, those new to Bash can think of it as a famous shell and scripting language in Unix-based operating systems. Using Bash, we can take advantage of multiple features and commands that assist users in automation and system administration tasks. Are […]
19 FebruaryRemove Newline from String in Bash
Using tr Command Use the tr command to remove the new line from a string. The tr command is used for character translation or deletion. echo "Hello World" | tr -d '\n' HelloWorld In the above, we have a string that contains a newline character between Hello and World. The echo command displays the string, […]