Questions tagged [bash]

Bash is the Bourne Again SHell, the successor to the classic Unix sh (shell).

bash is the Bourne Again SHell, the successor to the classic Unix Bourne shell (sh). It's the default shell on many Linux distributions, including RedHat, CentOS, Debian, and Ubuntu.

The premier online guide is the BashGuide.

3556 questions
206
votes
9 answers

Check if array is empty in Bash

I have an array which gets filled with different error messages as my script runs. I need a way to check if it is empty of not at the end of the script and take a specific action if it is. I have already tried treating it like a normal VAR and using…
180
votes
11 answers

Run an interactive bash subshell with initial commands without returning to the ("super") shell immediately

I want to run a bash subshell, (1) run a few commands, (2) and then remain in that subshell to do as I please. I can do each of these individually: Run command using -c flag: $> bash -c "ls; pwd; " however, it immediately…
135
votes
5 answers

Clean way to write complex multi-line string to a variable

I need to write some complex xml to a variable inside a bash script. The xml needs to be readable inside the bash script as this is where the xml fragment will live, it's not being read from another file or source. So my question is this if I have a…
ChrisInCambo
  • 1,771
71
votes
4 answers

Is there a difference between how two ampersands and a semi-colon operate in bash?

If I wanted to run two separate commands on one line, I could do this: cd /home; ls -al or this: cd /home && ls -al And I get the same results. However, what is going on in the background with these two methods? What is the functional difference…
Sean P
  • 727
40
votes
6 answers

bash script: repeat command if it returns an error

I would like to create a loop that repeats a ncftp transfer if it returns an error. I'm a little unsure how the exit code variable can be used in a loop. Would something like this work? until [$? == 0]; do ncftpput -DD -z -u user -p password…
Roy
  • 4,446
29
votes
2 answers

Bash script count down 5 minutes display on single line

I would like to have a count down of 5 minutes, updating every second and showing the result on the same line. Is this even possible with Bash scripting?
ptheofan
  • 411
20
votes
2 answers

How can I get remaining args after pulling out parsed items using getopts?

I want to parse some arguments to a bash script using getopts but want to be able to access the remaining args that are not included in the option list. So for example, if I have a call: % script -a -b param -c param -d other arguments here I would…
Tim
  • 1,899
20
votes
2 answers

Implementing dry-run in bash scripts

How would one implement a dry-run option in a bash script? I can think of either wrapping every single command in an if and echoing out the command instead of running it if the script is running with dry-run. Another way would be to define a…
19
votes
2 answers

What does "--" (double dash) mean in this shell command?

I have this shell command: kill `cat -- $PIDFILE` What the double -- does here? Why not use just kill `cat $PIDFILE`
daniels
  • 1,215
17
votes
5 answers

Check if directory exists using home character (~) in bash fails

Why does the following bash check if a directory fail? if [ ! -d "~/Desktop" ]; then echo "DOES NOT EXIST" exit 1; fi ~/Desktop does indeed exist. This is on a Mac by the way. The problem is with this type of script read -p "Provide the…
Justin
  • 5,438
16
votes
2 answers

Is there a bash builtin for 'which'?

I've been testing a minimal Fedora install. To check the path for interpreters like python or node, I normally use which. I notice which isn't installed by default. I could add the package, but I wonder if there's a shell builtin that can be used to…
mikemaccana
  • 3,470
13
votes
1 answer

How to kill all python processes except one from bash

How can I kill from bash all python processes excluding one python script. (I know its name, but its pid can be changed sometimes). I need kind of pkill -f "python" but with excluding the python specific script. Please advise.
13
votes
5 answers

Easy way to limit file size (stdout) on a shell script level?

Ok, this is a very practial use case from my point of view. Let say I have a some simple shell oneliner which does log the output into a file. This can be simply anything, for example tcpdump. Is there any generic and trivial way, to make sure, that…
12
votes
2 answers

How to echo a EOF in bash?

After search it seems the ascii of EOF is -1,but how can I echo it out? My purpose of doing this is to test whether it behaves the same as pressing ctrl-d if I just echo out EOF.
locale
  • 393
10
votes
1 answer

Job control: How to kill a sudo job using the job id

If I run jobs with sudo I can't kill %1 them (Operation not permitted). My first thought was to use sudo kill %1 instead, but that of course doesn't work either because it won't use the bash builtin kill. Is there a trick to make this work? // I…
S1lentSt0rm
  • 1,079
1
2 3
11 12