Tag archives for shell
How to concatenate string variables in Bash
Here are few preferred ways to concatenate string variables in bash script Concatenate string variables in Bash the variable one after another in echo #!/bin/bash var1="Techieroop" var2="${var1} Tutorial" echo "${var2}" ##output Techieroop Tutorial 2. Use += assignment operator: #!/bin/bash var="Techieroop" var+=" Tutorial" echo "$var" ##output Techieroop Tutorial Second method can be…
How to check if a string contains a substring in Bash
Here are the couple of methods to check if a string contains a substring in bash shell script. Methods to check if string contains a substring in Bash Method 1: Use regular expression #!/bin/bash str='Techieroop: Solution for DevOps & Open Source Technology' if [[ $str =~ "DevOps" ]]; then echo…
Shell Script Questions and Answers
Please find below list of Shell Script Questions and Answers. Shell Script Questions and Answers 1. Given two integers, X and Y , find their sum, difference, product, and quotient. Constraints X >= -100 Y <= 100 and Y != 100 read X case ${X#} in ** | '') echo "Not an…
Check if array contains value in shell script
Check if array contains value in shell script Here is simple bash script to check if array contains value . We can iterate over array and check if value is exist or not but we can also check in if condition if array contains value or not. Use this solution,…
How to get process ID of background process?
I usually prefer to execute process in background so that we don't need to worry if terminal got killed due to timeout or accidently. However, what if you want to kill the process then definitely you will you PID of that process to kill it. You may be interested to…
How to Split String with a Delimiter in Shell Script
Split String with a Delimiter in Shell Script You can use Internal Field Separator (IFS) variable in shell script to split string into array. When we set IFS variable then the assignment to IFS only takes place to that single command's environment to read. It then parses the input according…
How to validate date format in shell script
How to validate date format in shell script There are multiple ways you can validate date format in shell script. You can compare date format with regular expression or you can use inbuilt date command to check given date format is valid or not. I am sharing a simple date…
Loop through a date range in Shell Script
Loop through a date range in Shell Script Here is a simple shell script which accept two date value as argument and perform desired action for date value in #!/usr/bin/bash start=$1 end=$2 start=$(date -d $start +%Y%m%d) end=$(date -d $end +%Y%m%d) while ] do echo $start start=$(date -d"$start + 1 day"…
Pass date as mandatory arguments in bash script
Pass date as mandatory arguments in bash script I came across a use case where I have to write a shell script to perform some action on two date ranges. The first argument would start date and the second argument as end date. To validate missing argument we can also…
How to define multiline string variable in shell?
How to define multiline string variable in shell? In your bash/shell scripting experience you may face scenario where you need to define multi line string variable. Here is multiple ways to define multi line string variable in shell. Method 1: Combine Multiline with \n like below and echo with -e…








