9,012 questions
343
votes
4
answers
194k
views
When to wrap quotes around a shell variable?
Should or should I not wrap quotes around variables in a shell script?
For example, is the following correct:
xdg-open $URL
[ $? -eq 2 ]
or
xdg-open "$URL"
[ "$?" -eq "2"...
963
votes
7
answers
475k
views
Difference between single and double quotes in Bash
In Bash, what are the differences between single quotes ('') and double quotes ("")?
6278
votes
66
answers
5.2m
views
How do I execute a program or call a system command?
How do I call an external command within Python as if I had typed it in a shell or command prompt?
2507
votes
16
answers
3.1m
views
How do I set a variable to the output of a command in Bash?
I have a pretty simple script that is something like the following:
#!/bin/bash
VAR1="$1"
MOREF='sudo run command against $VAR1 | grep name | cut -c7-'
echo $MOREF
When I run this script from the ...
179
votes
7
answers
476k
views
I just assigned a variable, but echo $variable shows something else
Here are a series of cases where echo $var can show a different value than what was just assigned. This happens regardless of whether the assigned value was "double quoted", 'single quoted' or ...
416
votes
7
answers
746k
views
How do I use shell variables in an awk script?
I found some ways to pass external shell variables to an awk script, but I'm confused about ' and ".
First, I tried with a shell script:
$ v=123test
$ echo $v
123test
$ echo "$v"
123test
Then tried ...
1919
votes
11
answers
887k
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 ...
169
votes
9
answers
57k
views
Useless use of cat?
This is probably in many FAQs - instead of using:
cat file | command
(which is called useless use of cat), correct way supposed to be:
command < file
In the 2nd, "correct" way - OS does not have ...
323
votes
6
answers
109k
views
Correct Bash and shell script variable capitalization
I run across many shell scripts with variables in all caps, and I've always thought that there is a severe misunderstanding with that. My understanding is that, by convention (and perhaps by ...
729
votes
6
answers
481k
views
Command not found error in Bash variable assignment
I have this script called test.sh:
#!/bin/bash
STR = "Hello World"
echo $STR
when I run sh test.sh I get this:
test.sh: line 2: STR: command not found
What am I doing wrong? I look at extremely ...
1487
votes
25
answers
2.0m
views
Running shell command and capturing the output
I want to write a function that will execute a shell command and return its output as a string, no matter, is it an error or success message. I just want to get the same result that I would have ...
3032
votes
40
answers
3.9m
views
How do I split a string on a delimiter in Bash?
I have this string stored in a variable:
IN="[email protected];[email protected]"
Now I would like to split the strings by ; delimiter so that I have:
ADDR1="[email protected]"
ADDR2="[email protected]"
I don't ...
2273
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 ...
115
votes
10
answers
132k
views
How to print lines between two patterns, inclusive or exclusive (in sed, AWK or Perl)?
I have a file like the following and I would like to print the lines between two given patterns PAT1 and PAT2.
1
2
PAT1
3 - first block
4
PAT2
5
6
PAT1
7 - second block
PAT2
8
9
PAT1
10 - ...
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 ...