Linked Questions
76 questions linked to/from How to check if a string contains a substring in Bash
56
votes
8
answers
232k
views
How to find substring inside a string (or how to grep a variable)? [duplicate]
I'm using BASH, and I don't know how to find a substring. It keeps failing, I've got a string (should this be an array?)
Below, LIST is a string list of database names, SOURCE is the reply, one of ...
14
votes
2
answers
36k
views
Bash compare a command output to string [duplicate]
Output is same, and it always echos need to pull.
If I remove the quotes around $text in if condition it throws the too many arguments error.
var="$(git status -uno)" &&
text="On branch ...
11
votes
8
answers
12k
views
Check if a string contains asterisk (*) [duplicate]
I want to check if my string contains one or more asterisks.
I have tried this:
if [[ $date_alarm =~ .*\*.* ]]
then
...
fi
It worked when I launch the script directly, but not if this script is ...
4
votes
2
answers
6k
views
Substring of string matching regex in a bash shell [duplicate]
In a bash shell, I want to take the take a given string that matches a regex, and then take the part of the string.
For example, given https://github.com/PatrickConway/repo-name.git, I want to ...
12
votes
3
answers
374
views
Finding a string in a varaible with if statement [duplicate]
I am currently trying to find a string within a variable that outputs something like this:
one,two,three
My code:
echo "please enter one,two or three)
read var
var1=one,two,threee
if [[ "$var" == ...
0
votes
3
answers
2k
views
how to use if to see whether file has suffix in shell bash script [duplicate]
if[ xxx ]
how to expresss the string or file include '.'
I am new to study shell,thanks for any help
0
votes
2
answers
1k
views
How to check if array element is substring of string in Bash [duplicate]
This question does not answer my question.
I have a script that checks the existence of specific environment variables and prints them with their values. Now I want to mask the values of password ...
-2
votes
2
answers
489
views
How to check if a string contains a substring and does not in Bash [duplicate]
I am wanting to use a switch/ case statement in bash to check if a file name which is a string contains something but also does not.
Here is my case:
case "$fileName" in
*Failed|!cp*)
...
1
vote
1
answer
399
views
Error too many arguments trying to match substring in string bash [duplicate]
I have the following piece of code where I am trying to match the word "test" in the given string:
str="some test string"
if [ $str == *"test"* ]; then # [: too many ...
0
votes
3
answers
468
views
bash / Check if number matches number in list [duplicate]
I'm trying to write a small backupscript which should get executed every hour via cronjob.
While the script does monthly/weekly/daily backups and these work, i also would like to perform hourly ...
1
vote
1
answer
421
views
Bash script if condition [duplicate]
I am trying to understand a piece of code here and as I am not an expert on bash, I am struggling to find out what the below if does:
if [[ "4ドル" == *"-a "* ]]; then
I understand ...
1
vote
3
answers
131
views
Check in shellscript if a variable is partially the same as a parameter [duplicate]
So this is admittedly for university, but I can't find the answer anywhere, nor online, nor in the lecture notes.
I basically take a parameter, and have to search, if that is part of a longer string ...
3
votes
0
answers
226
views
how to check if a variable contains a value in bash shell [duplicate]
Am having difficulty checking if the contents of a variable contains a certain value or not e.g "Device_info"
The main code
if [ "${NUMBERRESULTS}" -eq 10 ]
then
echo "Success"
else
echo "...
1
vote
2
answers
82
views
search for a variable in a string in bash [duplicate]
I have a string given below:
string1 = "Hello there, my name is Jack.
Hello there, my name is Jack.
Hello there, my name is Jack."
I'm taking the following input from the string:
read ...
1
vote
0
answers
73
views
Is there a way in Bash to determine if a character exists in a string? [duplicate]
Similar to how in python you can do 'if letter in string:', I'm trying to imitate this in bash. The code is below:
SYMBOLS="ABC"
msg="alskdfjAasdksdfh!?asdflkjhghrg"
# Loops through each letter in ...