Questions tagged [shell]
The term 'shell' refers to a general class of text-based interactive command interpreters most often associated with the Unix & Linux operating systems. For questions about shell scripting, please use a more specific tag such as 'bash', 'powershell' or 'ksh'. Without a specific tag, a portable (POSIX-compliant) solution should be assumed, though using 'posix' in addition or 'sh' instead is preferable.
shell
8,684
questions
321
votes
4
answers
171k
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"...
914
votes
7
answers
441k
views
Difference between single and double quotes in Bash
In Bash, what are the differences between single quotes ('') and double quotes ("")?
6173
votes
66
answers
4.8m
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?
2435
votes
16
answers
2.9m
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 ...
168
votes
7
answers
408k
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 ...
402
votes
7
answers
703k
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 ...
1867
votes
11
answers
825k
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 main difference between Bash and sh?
What do we need to ...
157
votes
9
answers
52k
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 ...
302
votes
9
answers
98k
views
Correct Bash and shell script variable capitalization [closed]
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 ...
715
votes
6
answers
466k
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 ...
1437
votes
25
answers
1.9m
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 ...
2946
votes
39
answers
3.6m
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 ...
2202
votes
20
answers
1.8m
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 ...
103
votes
9
answers
118k
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 - ...
27
votes
2
answers
11k
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 ...