Techolac - Computer Technology News
  • Home
  • Internet
  • Business
  • Computers
  • Gadgets
  • Lifestyle
  • Phones
  • Travel
  • Tech
  • More
    • Automotive
    • Education
    • Entertainment
    • Health
    • SEO
    • Linux
    • WordPress
    • Home Improvement
    • How to
    • Games
No Result
View All Result
  • Home
  • Internet
  • Business
  • Computers
  • Gadgets
  • Lifestyle
  • Phones
  • Travel
  • Tech
  • More
    • Automotive
    • Education
    • Entertainment
    • Health
    • SEO
    • Linux
    • WordPress
    • Home Improvement
    • How to
    • Games
No Result
View All Result
Techolac - Computer Technology News
No Result
View All Result
Home Linux

Bash check if string starts with character such as #

by Editorial Staff
December 14, 2018
in Linux
Reading Time: 6 mins read

[ad_1]

My bash shell script read a config file line-by-line using a bash while loop. I need to check if a string stored in $var starts with some value such as # character. If so I need to ignore that value and read the next line from my config file. How do I check if variable begins with # in bash shell scripting running on a Linux or Unix-like operating systems?

Introduction – In bash, we can check if a string begins with some value using regex comparison operator =~. One can test that a bash variable starts with a string or character in bash efficiently using any one of the following methods.

How to check if a string begins with some value in bash

Let us define a shell variable called vech as follows:
vech="Bus"
To check if string “Bus” stored in $vech starts with “B”, run:
[[ $vech = B* ]] && echo "Start with B"
The [[ used to execute the conditional command. It checks if $vech starts with “B” followed by any character. Set vech to something else and try it again:
vech="Car"
[[ $vech = B* ]] && echo "Start with B"
[[ $vech = B* ]] && echo "Start with B" || echo "Not matched"

Bash check if string starts with character using if statement

if..else..fi allows to make choice based on the success or failure of a command:

#!/bin/bash
input="xBus"
 
if [[ $input = B* ]]
then
	echo "Start with B"
else
	echo "No match"
fi

#!/bin/bash
input=”xBus” if [[ $input = B* ]]
then
echo “Start with B”
else
echo “No match”
fi

Bash check if a variable string begins with # value

The syntax is as follows to read a file line-by-line:

#!/bin/bash
input="/path/to/txt/file"
while IFS= read -r var
do
  echo "$var"
done < "$input"

#!/bin/bash
input=”/path/to/txt/file”
while IFS= read -r var
do
echo “$var”
done < “$input”

Let us add check to see if $var starts with “#” in bash:

See also  How Common Signals are Used in Kubernetes Pod Management
#!/bin/bash
input="/path/to/txt/file"
while IFS= read -r var
do
  #
  # if value of $var starts with #, ignore it
  #
  [[ $var =~ ^#.* ]] && continue
  echo "$var"
done < "$input"

#!/bin/bash
input=”/path/to/txt/file”
while IFS= read -r var
do
#
# if value of $var starts with #, ignore it
#
[[ $var =~ ^#.* ]] && continue
echo “$var”
done < “$input”

The continue statement resumes the next iteration of the enclosing while loop, so it skips rest of the commands when commented line found out.

How to use regex comparison operator =~ if string starts with character

The syntax is as follows to see if $var starts with “#”:

if [[ "$var" =~ ^#.*  ]]; then
    echo "yes"
fi

if [[ “$var” =~ ^#.* ]]; then
echo “yes”
fi

Therefor the following is an updated version:

while IFS='|' read -r t u
do
        # ignore all config line starting with '#'
	[[ $t =~ ^#.* ]] && continue
        echo "Working on $t and $u" 
 
done < "$INPUT_FILE"

while IFS=’|’ read -r t u
do
# ignore all config line starting with ‘#’
[[ $t =~ ^#.* ]] && continue
echo “Working on $t and $u”
done < “$INPUT_FILE”

How to check if a string begins with some value in bash

The case statement is good alternative to multilevel if-then-else-fi statement. It enable you to match several values against one variable. It is easier to read and write:

#!/bin/bash
# set default to 'Bus' but accept the CLI arg for testing
input="${1:-Bus}" 
echo -n "$input starts with 'B' : "
case "$input" in
    B*)
            echo "yes";;
     *)
            echo "no";;
esac

#!/bin/bash
# set default to ‘Bus’ but accept the CLI arg for testing
input=”${1:-Bus}”
echo -n “$input starts with ‘B’ : ”
case “$input” in
B*)
echo “yes”;;
*)
echo “no”;;
esac

Linux and Unix bash check if variable start with

Conclusion

You learned how to check if a string begins with some value using various methods. For more info read the bash command man page here.

See also  12 Useful ‘dmidecode’ Command Examples for Linux Systems

Posted by: Vivek Gite

The author is the creator of nixCraft and a seasoned sysadmin, DevOps engineer, and a trainer for the Linux operating system/Unix shell scripting. Get the latest tutorials on SysAdmin, Linux/Unix and open source topics via RSS/XML feed or weekly email newsletter.

[ad_2]

Related Posts

Top Commands to Check the Running Processes in Linux
Linux

Top Commands to Check the Running Processes in Linux

May 27, 2022
4 Tips to Prevent and Troubleshoot the ImagePullBackOff Kubernetes Error
Linux

4 Tips to Prevent and Troubleshoot the ImagePullBackOff Kubernetes Error

March 30, 2022
How Common Signals are Used in Kubernetes Pod Management
Linux

How Common Signals are Used in Kubernetes Pod Management

November 20, 2021

Leave a Reply Cancel reply

Your email address will not be published. Required fields are marked *

Bitcoin Cloud Mining Sites

12 Best Bitcoin Cloud Mining Sites In 2022

August 9, 2022
Why did the Parag Parikh Long Term Equity Fund change its name to Parag Parikh Flexi Cap Fund?

Why did the Parag Parikh Long Term Equity Fund change its name to Parag Parikh Flexi Cap Fund?

August 9, 2022
Why Measuring Customer Experience Score Generate More Business Value?

Why Measuring Customer Experience Score Generate More Business Value?

August 9, 2022
The Key Phases for Successful Onboarding

The Key Phases for Successful Onboarding

August 9, 2022
30 ZiniTevi Alternatives To Watch Movies And TV Shows Online

30 ZiniTevi Alternatives To Watch Movies And TV Shows Online

August 8, 2022
  • DashTech
  • TechDaddy
  • Terms and Conditions
  • Disclaimer
  • Write for us

© Techolac © Copyright 2019 - 2022, All Rights Reserved.

No Result
View All Result
  • Home
  • Internet
  • Business
  • Computers
  • Gadgets
  • Lifestyle
  • Phones
  • Travel
  • Tech
  • More
    • Automotive
    • Education
    • Entertainment
    • Health
    • SEO
    • Linux
    • WordPress
    • Home Improvement
    • How to
    • Games

© Techolac © Copyright 2019 - 2022, All Rights Reserved.

Go to mobile version