Linked Questions
28 questions linked to/from What is the difference between single and double square brackets in Bash?
13 votes
2 answers
10k views
Meaning of double square brackets in bash [duplicate]
At this question in the answer from Kyle Brandt the [[ ]] construct is described as a "bash built-in test command". The following is shown as an example. if [[ ( $a -gt 2 ) && ( $a -lt 5 )...
0 votes
0 answers
3k views
how to detect if a string ends with " in bash [duplicate]
I have a problem with bash, I'm trying to detect strings in a file with bash with the file looking like cnlog "Hello World!" When i try the [ $variable = *" ] in bash it doesn't work ...
2 votes
1 answer
572 views
Validating user fed time in bash with regex pattern [duplicate]
Assume a user fed time : read -p "Enter the start time you want to start(hh:mm:ss) " user_start_time and the regex : timePattern="[0-2][0-3]:[0-5][0-9]:[0-5][0-9]" if i do : if [ "$user_start_time"...
-1 votes
1 answer
224 views
Bash Script incorrectly matching all strings with regular expression [duplicate]
I was trying to use a bash script to validate some strings. No matter what I've tried, the script ends up matching every string provided as input and it's printing yes in the terminal. For example, ...
0 votes
0 answers
62 views
How to only accept input that is a valid index of an array in bash [duplicate]
I am trying some simple input validation in bash. Basically this part of my script outputs "press [index] to select [array element]." However I cannot seem to get it to stop and exit ...
0 votes
0 answers
49 views
Glob pattern condition in if statement [duplicate]
I have a simple for-loop that iterates over a glob-pattern and enters an if condition, for when the directory path is not equal to */venv then print the file. How can I get this working in the if ...
2270 votes
21 answers
1.9m views
How do I iterate over a range of numbers defined by variables in Bash?
How do I iterate over a range of numbers in Bash when the range is given by a variable? I know I can do this (called "sequence expression" in the Bash documentation): for i in {1..5}; do ...
1915 votes
11 answers
884k views
Difference between sh and Bash
When writing shell programs, we often use /bin/sh and /bin/bash. I usually use bash, but I don't know what's the difference between them. What's the main difference between Bash and sh? What do we ...
342 votes
9 answers
263k views
What is the $? (dollar question mark) variable in shell scripting? [duplicate]
I'm trying to learn shell scripting, and I need to understand someone else's code. What is the $? variable hold? I can't Google search the answer because they block punctuation characters.
357 votes
8 answers
338k views
Meaning of $? (dollar question mark) in shell scripts
What does echo $? mean in shell programming?
266 votes
7 answers
224k views
Test for non-zero length string in Bash: [ -n "$var" ] or [ "$var" ]
I've seen Bash scripts test for a non-zero length string in two different ways. Most scripts use the -n option: #!/bin/bash # With the -n option if [ -n "$var" ]; then # Do something when var is ...
113 votes
4 answers
145k views
Getting "command not found" error while comparing two strings in Bash
My whole script is currently this: #!/bin/sh clear; blanko=""; # Dummy-Variablen variable=Testvariable; if [[$variable == $blanko]]; then echo "Nichts da!" else ...
49 votes
6 answers
43k views
How to test strings for lexicographic less than or equal in Bash?
In Bash, is there a simple way to test if one string is lexicographically less than or equal to another? I know you can do: if [[ "a" < "b" ]] for testing strict inequality, or if [[ 1 -le 1 ]] ...
39 votes
2 answers
15k views
Why is testing "$?" to see if a command succeeded or not, an anti-pattern?
I see here that testing whether $? is zero (success) or something else (failure) is an anti-pattern, but I have not been able to find this anywhere else. Sticking to the definition of anti-pattern of ...
0 votes
1 answer
6k views
What is wrong with my guessing game program?
So yes I am doing this for school but I have most of the script written. I don't know what's going on with it, maybe a syntax error but it keeps messing up(or I do). The first issue is that it keeps ...