Questions tagged [bash-scripting]

Bash scripting is making scripts in the Bash shell language.

Bash has several features which allow making small uncompiled programs (scripts) to automate functionality. It features basic programming constructs like variables, IF-statements and loops.

Bash scripts are usually created in files with the .sh extension. To make the file executable as it were a program, put the following line at the top of the file:

#!/bin/bash

This will instruct the kernel to start a Bash shell and use the rest of the file as input. After adding the 'shebang' line, change the permissions of the file to be executable with chmod +x filename.sh. You can then run the script with ./filename.sh.

Example IF-statement

VARIABLE1='hello world'
if [[ $VARIABLE1 == 'hello world' ]]
then
  echo 'Hello World!' 
fi

In this example we see:

  • Variable $VARIABLE1 is set to the value hello world
  • The content of $VARIABLE1 is matched against the string hello world
  • If this is true, then print Hello World! to the screen.

Example Loop

Several kinds of loops can created in bash:

For loop

VARIABLE1="anna bob charlie dave eve"
for NAME in $VARIABLE1
do
  echo $NAME
done

The for statement will iterate through the names in $VARIABLE1 and run the code block between do and done for each name. This will output all 5 names separated by newlines.

You can also create a C-style for-loop:

for ((i = 0 ; i < 10 ; i++)); do
  echo $i
done

This will print numbers 1 through 9 separated by newlines.

While loop

VARIABLE1=1

while [ $VARIABLE1 -lt 10 ]
do
  echo "$VARIABLE1"
  VARIABLE1=$[$VARIABLE1+1]
done
970 questions
82
votes
3 answers

ZSH: Read command fails within bash function "read:1: -p: no coprocess"

Edit: Seems to work within bash. It appears the problem is related to zsh. If there is a better site to post this issue on let me know. I am writing a simple script that creates a series of directories. I want the user to give a confirmation…
Nick Tomlin
  • 1,157
80
votes
4 answers

how to grep and print the next N lines after the hit?

I would like to grep for an occurrence in a text file, then print the following N lines after each occurrence found. Any ideas?
719016
  • 4,683
54
votes
10 answers

Suppress execution trace for echo command?

I'm running shell scripts from Jenkins, which kicks off shell scripts with the shebang options #!/bin/sh -ex. According to Bash Shebang for dummies?, -x, "causes the shell to print an execution trace", which is great for most purposes - except for…
Christian
  • 811
40
votes
2 answers

How can I compare a variable to a text string, rather than integer, in an if/else statement?

In the process of writing a shell script, I ran into an issue with the following if/else statement that falls somewhere in the middle of the script: if [ $act -eq "add" ] then read - "add or update: " $comm git commit -m "$comm $file" else …
37
votes
1 answer

Bash: optionally passing arguments to a command

I'm trying to add arguments to a command call depend on another variable. Please look at the shell scripting code: curl \ $([ -z "${title}" ] || echo --data-urlencode title=${title}) \ http://example.com In the example, if title is given not…
32
votes
5 answers

Passing two arguments to a command using pipes

Usually, we only need to pass one argument: echo abc | cat echo abc | cat some_file - echo abc | cat - some_file Is there a way to pass two arguments? Something like {echo abc , echo xyz} | cat cat `echo abc` `echo xyz` I could just store both…
31
votes
2 answers

Replace backslash("\") with forward slash("/") in a variable in bash

I want to replace backslash(\) with forward slash(/) in a variable in bash. I tried it like this, but it doesn't work: home_mf = ${home//(\)//(/)} For example, I would like \a\b\c -> /a/b/c
Para
  • 423
27
votes
4 answers

Is it safe to open a file that is being written by a running script?

I am running a shell script that is writing to a file. This script may take a long time to complete and I would like to monitor the partial output rather than wait for the whole script to finish. Is it safe to open (double click) a file that is…
None
  • 589
26
votes
3 answers

How to update bash on Mac OS X Yosemite

Just trying to learn bash scripting a little. My old bash version: Bash version 3.2.53(1)-release... I've updated my bash on mac os x yosemite with homebrew: brew update brew install bash Then in terminal properties I’ve changed the standard shell…
drew1kun
  • 2,207
24
votes
4 answers

Edit xml file using shell script / command

I need to do this using unix script or command There is a xml file in /home/user/app/xmlfiles like
23
votes
2 answers

Writing shell scripts that will run on any shell (using multiple shebang lines?)

I've just started getting deeper into shell scripting, and I've always just thrown my script in a file, marked it chmod +x and then done /path/to/script.sh and let whatever interpreter is the default have its way with it, which I assumed was zsh…
swrobel
  • 365
22
votes
4 answers

appending timestamp to tail -f results

I would like to have a timestamp printed in front of every line that comes out of a "tail -f outputfile", so that I know when is each line printed in. Something like: [...] 20110617_070222:results printed here 20110617_070312:results printed…
719016
  • 4,683
20
votes
1 answer

How to change user ID inside a bash script and execute commands as that other user

Is there a way to switch user identity within a script (executed as root as part of an installation process) to execute some commands without calling an external script, then return to root to run other commands? Sort of: #!/bin/bash some commands…
a1an
  • 365
  • 1
  • 2
  • 9
19
votes
3 answers

Prefix to each output of a command on runtime

I am trying to make a modular script. I have several scripts/commands which are called from a single script. I want to prefix the output of each separate command. Examle: My files are allcommands.sh / command1.sh / command2.sh command1.sh…
Ivan Dokov
  • 321
  • 1
  • 2
  • 7
1
2 3
64 65