Tagged Questions
145
votes
10answers
13k views
In Bash, when to alias, when to script, and when to write a function?
It's taken me almost 10 years of Linux usage to ask this question. It was all trial and error and random late-night internet surfing.
But people shouldn't need 10 years for this. If I were just ...
52
votes
1answer
2k views
Have backticks (i.e. `cmd`) in *sh shells been deprecated?
I've seen this comment many times on Unix & Linux as well as on other sites that use the phrasing "backticks have been deprecated", with respect to shells such as Bash & Zsh.
Is this ...
31
votes
3answers
9k views
Multiline shell script comments - how does this work?
Recently, I stumbled upon a multiline comment type I have never seen before - here is a script example:
echo a
#
: aaa
: ddd
#
echo b
This seems to work, hell, even vim syntax-highlights it. What ...
27
votes
5answers
2k views
Better way of accepting variations of ‘Yes’ from a shell prompt
Usability testing of a shell script I wrote found that people had different expectations on how to answer a question that expected ‘yes’ as the answer. See variations in the below code example.
...
23
votes
9answers
893 views
for vs find in Bash
When looping through files there are two ways:
1. use a for-loop
for f in *; do
echo "$f"
done
2. use find
find * -prune | while read f; do
echo "$f"
done
Assuming these two loops will ...
23
votes
7answers
909 views
Should scripts that require sudo fail if they don't have it, or use sudo and prompt?
I have a script which gives me fine-grained control over my backlight brightness and requires sudo to run. It's essentially this:
backlight="/sys/class/backlight/acpi_video0/brightness"
echo $1 | tee ...
23
votes
4answers
4k views
How to do nothing forever in an elegant way?
I have a program which produces useful information on stdout but also reads from stdin. I want to redirect its standard output to a file without providing anything on standard input. So far, so good: ...
18
votes
3answers
6k views
What does it mean to have a $“dollarsign-prefixed string” in a script?
I just saw this in an init script:
echo $"Stopping Apache"
What is that dollar-sign for?
My research so far:
I found this in the bash manual:
extquote
If set, $'string' and ...
18
votes
2answers
580 views
Why does my shell script choke on whitespace or other special characters?
Or, an introductory guide to robust filename handling and other string passing
in shell scripts.
I wrote a shell script which works well most of the time. But it chokes on some
inputs (e.g. on some ...
18
votes
1answer
2k views
Execute bash scripts on entering a directory
What is the best way to execute a script when entering into a directory? When I move into a new directory I would like bash to execute the projectSettings.bash script much like RVM does.
17
votes
2answers
2k views
What is the difference between running “bash script.sh” and “./script.sh”?
If script.sh is just something typical like
#!/bin/bash
echo "Hello World!"
Is there a preferred way to run the script? I think you first have to chmod it so it becomes executable?
17
votes
3answers
6k views
How do I exit a script in a conditional statement?
I'm writing a bash script where I want to exit if the user is not root. The conditional works fine, but the script does not exit.
[[ `id -u` == 0 ]] || (echo "Must be root to run script"; exit)
...
17
votes
2answers
2k views
aliasing cd to pushd - is it a good idea?
Is it a good idea to use the following alias:
cd() {
pushd $1;
}
in bash?
I think this would be very useful, since I can then use a series of popds instead of just a cd - once.
Is there any ...
15
votes
3answers
696 views
What is the difference between [[ $a == z* ]] and [ $a == z* ]?
Is there is any difference between these two.
[[ $a == z* ]]
and
[ $a == z* ]
Can I have an example where they would have different outputs?
Furthermore, how does the working of [[ ]] differs ...
14
votes
2answers
1k views
In bash, is it possible to use an integer variable in the loop control of a for loop?
I have the following bash script:
#!/bin/bash
upperlim=10
for i in {0..10}
do
echo $i
done
for i in {0..$upperlim}
do
echo $i
done
The first for loop (without the variable upperlim in the loop ...
14
votes
1answer
2k views
Can bash case statements cascade?
I'm trying to do something like this:
case $level in
3)
echo "Level Three"
2)
echo "Level Two"
1)
echo "Level one"
;;
esac
where if $level = 3, it ...
13
votes
3answers
3k views
How can one run multiple programs in the background with single command?
How can one run multiple programs in the background with single command?
I have tried the commands below, but they do not work.
nohup ./script1.sh & && nohup ./script2.sh &
-bash: ...
13
votes
5answers
449 views
Stateful bash function
I’d like to implement a function in Bash which increases (and returns) a count with every call. Unfortunately this seems non-trivial since I’m invoking the function inside a subshell and it ...
13
votes
2answers
719 views
running script with “. ” and with “source ”
I was wondering if the following two ways of running a bash script
are equivalent?
. ./myScript.sh
source myScript.sh
Are they both running the content of the script instead of running the script, ...
13
votes
4answers
2k views
How to make bash abort the execution of a script on syntax error?
To be on safe side, I'd like bash abort the execution of a script if it encounters a syntax error.
To my surprise, I can't achieve this. (set -e is not enough.) Example:
#!/bin/bash
# Do exit on ...
13
votes
2answers
3k views
Rule for invoking subshell in Bash?
I seem to misunderstand the Bash rule for creating a subshell. I thought parentheses always creates a subshell, which runs as its own process.
However, this doesn't seem to be the case. In Code ...
12
votes
2answers
633 views
When to use a semi-colon between environment variables and a command
Can anyone explain why the semi-colon is necessary in order for the LANG to be seen as updated by bash?
Doesn't work:
> LANG=Ja_JP bash -c "echo $LANG"
en_US
Works:
> LANG=Ja_JP ; bash -c ...
12
votes
1answer
725 views
When is double-quoting necessary?
The old advice used to be to double-quote any expression involving a $VARIABLE, at least if one wanted it to be interpreted by the shell as one single item, otherwise, any spaces in the content of ...
12
votes
2answers
1k views
How to safely pass variables to root-enabled scripts?
This question is totally general and not only applicable to my situation, but... I have a small busybox appliance where I want a non-root user to be able to execute a particular script with root ...
12
votes
3answers
16k views
Shell script fails: Syntax error: “(” unexpected
I've been working on a script that automates setting up a development environment for Raspberry Pi development (step by step details that work are here). The script is linked in that article but ...
12
votes
3answers
337 views
When to use redirection to stderr in shell scripts
I know that well-behaved utilities like grep output "normal" messages to stdout, and error messages to stderr.
$ grep '^foo' file1 file2
file1:foo
grep: file2: No such file or directory
When I'm ...
11
votes
5answers
2k views
Bash script to get ASCII values for alphabet
How do I get the ASCII value of the alphabet?
For example, 97 for a?
11
votes
3answers
23k views
How to measure time of program execution and store that inside a variable
In order to find out how long certain operations within a Bash (v4+) script take, I would like to parse the output from the time command "separately" and (ultimately) capture it within a Bash variable ...
11
votes
3answers
977 views
Variable as command; eval vs bash -c
I was reading a bash script someone made and I noticed that the author doesn't use eval to evaluate a variable as a command
The author used
bash -c "$1"
instead of
eval "$1"
I assume using eval ...
11
votes
4answers
4k views
How can I get bash to exit on backtick failure in a similar way to pipefail?
So I like to harden my bash scripts wherever I can (and when not able to delegate to a language like Python/Ruby) to ensure errors do not go uncaught.
In that vein I have a strict.sh, which contains ...
11
votes
1answer
689 views
How does a fork bomb work?
WARNING DO NOT ATTEMPT TO RUN THIS ON A PRODUCTION MACHINE
In reading the Wikipedia page on the topic I generally follow what's going on with the following code:
:(){ :|:& };:
excerpt of ...
10
votes
2answers
934 views
find and remove duplicates in a directory
I have a directory with multiple img files and some of them are identical but they all have different names. I need to remove duplicates but with no external tools only with a bash script. I'm a ...
10
votes
7answers
699 views
Test if multiple variables are set
I'd like to make sure that at a certain point of a script, after sourceing a configuration file, several variables are set and, if they are not, to stop execution, telling the user about the missing ...
10
votes
1answer
3k views
Are there naming conventions for variables in shell scripts?
Most languages have naming conventions for variables, the most common style I see in shell scripts is MY_VARIABLE=foo. Is this the convention or is it only for global variables? What about variables ...
9
votes
7answers
1k views
bash: test if $WORD is in set
I am looking for a construct in bash, to decide if a variable $WORD is one of defined words. I need something like this:
if "$WORD" in dog cat horse ; then
echo yes
else
echo no
fi
does ...
9
votes
2answers
1k views
Fastest way to concatenate files
I've got 10k+ files totaling over 20GB that I need to concatenate into one file.
Is there a faster way than
cat input_file* >> out
?
The preferred way would be a bash command, Python is ...
9
votes
3answers
3k views
How to sort the string which combined with string + numeric using bash script?
This is the data what I want to sort. But sort treats the numeric to string, the data it no sorted as I expected.
/home/files/profile1
/home/files/profile10
/home/files/profile11
...
9
votes
1answer
589 views
How do you omit “./” when running scripts on current directory
On some tutorials over the internet they can run a script on the current directory without doing:
./script
How do you omit this? I am using CentOS 5.6 x64. I want to run it this way
script
9
votes
3answers
2k views
Substituting strings in a very large file
I have a very long series of urls with no separating character, in the same format as below:
http://example.comhttp://example.nethttp://example.orghttp://etc...
I want each URL to be on a new line. ...
9
votes
2answers
3k views
Parallelizing a for loop
I want to parallelize the for loops of the following code. How to do this?
#!/bin/bash
N=$1
n=$2
for (( i=1; i<=$N; i++ )); do
min=100000000000000 //set min to some garbage value
for (( ...
9
votes
2answers
3k views
Regular expression in bash script
This is my first time bash scripting so I'm probably making an easy mistake.
Basically, I'm trying to write a script that gets the groups of a user, and if they are in a certain group, it will log ...
9
votes
1answer
5k views
How can I execute local script on remote machine and include arguments?
I have written a script that runs fine when executed locally:
./sysMole -time Aug 18 18
The arguments "-time", "Aug", "18", and "18" are successfully passed on to the script.
Now, this script is ...
9
votes
4answers
10k views
How can I use bash's if test and find commands together?
I have a directory with crash logs, and I'd like to use a conditional statement in a bash script based on a find command.
The log files are stored in this format:
/var/log/crashes/app-2012-08-28.log
...
9
votes
2answers
344 views
Are quotes needed for local variable assignment?
Can I safely omit quotes on the right side of a local assignment?
function foo {
local myvar=${bar}
stuff()
}
I'm mainly interested in bash, but any info on corner cases in other shells are ...
8
votes
5answers
2k views
Bash CD up until in certain folder
I do a lot of work in Magento, and so do a lot of the people I work with, and it gets annoying to have to type:
cd ../../../../../../
To only find you're still a few directories from your root ...
8
votes
4answers
558 views
Using “${a:-b}” for variable assignment in scripts
I have been looking at a few scripts other people wrote (specifically Red Hat) and a lot of their variables are assigned using the following notation
VARIABLE1="${VARIABLE1:-some_val}"
or some expand ...
8
votes
2answers
13k views
How do I delete the first n lines of an ascii file using shell commands?
I have multiple files that contain ascii text information in the first 5-10 lines, followed by well-tabulated matrix information. In a shell script, I want to remove these first few lines of text so ...
8
votes
1answer
12k views
How to check if there are no parameters provided to a command?
How do you check if $* is empty? In other words, how to check if there were no arguments provided to a command?
8
votes
4answers
7k views
Bash script testing if a command has run correctly
I am working on a bash script that I would like to work for several types of VCS.
I am thinking of testing if a directory is a repo for a system by running a typical info command and checking the ...
8
votes
1answer
6k views
File descriptors & shell scripting
I am having a very hard time understanding how does one use file descriptors in shell scripts.
I know the basics such as
exec 5 > /tmp/foo
So fd 5 is attached to foo for writing.
exec 6 < ...