Ad Clicks : Ad Views : Ad Clicks : Ad Views : Ad Clicks : Ad Views : Ad Clicks : Ad Views : Ad Clicks : Ad Views : Ad Clicks : Ad Views : Ad Clicks : Ad Views : Ad Clicks : Ad Views : Ad Clicks : Ad Views : Ad Clicks : Ad Views : Ad Clicks : Ad Views : Ad Clicks : Ad Views : Ad Clicks : Ad Views : Ad Clicks : Ad Views :
Oh Snap!

Please turnoff your ad blocking mode for viewing your site content

Generic selectors
Exact matches only
Search in title
Search in content
Search in posts
Search in pages
img
Home / Linux Tutorials / Bash examine if string begins with character corresponding to #

Bash examine if string begins with character corresponding to #

130 Views

My bash shell script learn a config file line-by-line utilizing a bash whereas loop. I must examine if a string saved in $var begins with some worth corresponding to # character. In that case I must ignore that worth and skim the subsequent line from my config file. How do I examine if variable begins with # in bash shell scripting working on a Linux or Unix-like working programs?

Introduction – In bash, we will examine if a string begins with some worth utilizing regex comparability operator =~. One can take a look at bash variable begins with a string or character in bash effectively utilizing any one of many following strategies.

The way to examine if a string begins with some worth in bash

Allow us to outline a shell variable referred to as vech as follows:
vech=”Bus”
To examine if string “Bus” saved in $vech begins with “B”, run:
[[ $vech = B* ]] && echo “Begin with B”
The [[ used to execute the conditional command. It checks if $vech begins with “B” adopted by any character. Set vech to one thing else and take a look at it once more:
vech=”Automotive”
[[ $vech = B* ]] && echo “Begin with B”
[[ $vech = B* ]] && echo “Begin with B” || echo “Not matched”

Bash examine if string begins with character utilizing if assertion

if..else..fi permits to make alternative primarily based on the success or failure of a command:

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

Bash examine if a variable string begins with # worth

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

#!/bin/bash
enter=“/path/to/txt/file”
whereas IFS= learn -r var
do
echo $var
performed < $enter

Allow us to add examine to see if $var begins with “#” in bash:

#!/bin/bash
enter=“/path/to/txt/file”
whereas IFS= learn -r var
do
#
# if worth of $var begins with #, ignore it
#
[[ $var =~ ^#.* ]] && proceed
echo $var
performed < $enter

The proceed assertion resumes the subsequent iteration of the enclosing whereas loop, so it skips remainder of the instructions when commented line discovered.

The way to use regex comparability operator =~ if string begins with character

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

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

Therefor the next is an up to date model:

whereas IFS=‘|’ learn -r t u
do
# ignore all config line beginning with ‘#’
[[ $t =~ ^#.* ]] && proceed
echo “Engaged on $t and $u
 
performed < $INPUT_FILE

The way to examine if a string begins with some worth in bash

The case assertion is sweet various to multilevel if-then-else-fi assertion. It allow you to match a number of values in opposition to one variable. It’s simpler to learn and write:

#!/bin/bash
# set default to ‘Bus’ however settle for the CLI arg for testing
enter=$
echo -n $enter begins with ‘B’ : “
case $enter in
B*)
echo “sure”;;
*)
echo “no”;;
esac

Linux and Unix bash check if variable start with

Conclusion

You realized examine if a string begins with some worth utilizing numerous strategies. For more information learn the bash command man web page right here.

Posted by: Vivek Gite

The writer is the creator of nixCraft and a seasoned sysadmin, DevOps engineer, and a coach for the Linux working system/Unix shell scripting. Get the newest tutorials on SysAdmin, Linux/Unix and open supply subjects through RSS/XML feed or weekly e mail publication.

Source link

  • Facebook

This div height required for enabling the sticky sidebar