Skip to main content

Questions tagged [bash-scripting]

Bash scripting is making scripts in the Bash shell language.

Filter by
Sorted by
Tagged with
76 votes
3 answers
45k views

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 ...
Nick Tomlin's user avatar
  • 1,097
65 votes
4 answers
189k views

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's user avatar
  • 4,427
50 votes
10 answers
75k views

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"...
Christian's user avatar
  • 771
40 votes
2 answers
133k views

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 ...
tony_perkis666's user avatar
36 votes
1 answer
11k views

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://...
shouya's user avatar
  • 2,657
32 votes
5 answers
61k views

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 ...
goweon's user avatar
  • 1,753
30 votes
1 answer
5k views

What is <<!END in a Bash script?

Consider: #!/bin/bash /u01/app/oracle/middleware/oracle_common/common/bin/wlst.sh <<!END connect('user','pw'); p=redeploy('application'); p.printStatus(); exit(); !END The above script is ...
Isaac's user avatar
  • 411
29 votes
2 answers
59k views

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's user avatar
  • 403
26 votes
3 answers
18k views

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 ...
drew1kun's user avatar
  • 2,157
25 votes
4 answers
23k views

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 ...
None's user avatar
  • 559
23 votes
4 answers
98k views

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 <book> <fiction type='a'> <author type=''></author> </...
VRVigneshwara's user avatar
23 votes
2 answers
9k views

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 ...
swrobel's user avatar
  • 365
20 votes
1 answer
28k views

Howto switch / chage user id witin a bash script to execute commands in the same script?

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 ...
a1an's user avatar
  • 365
19 votes
4 answers
13k views

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 ...
719016's user avatar
  • 4,427
17 votes
2 answers
25k views

Properly escaping forward slash in bash script for usage with sed

I'm trying to determine the size of the files that would be newly copied when syncing two folders by running rsync in dry mode and then summing up the sizes of the files listed in the output of rsync. ...
user331839's user avatar
17 votes
3 answers
12k views

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 ...
Ivan Dokov's user avatar
17 votes
4 answers
27k views

How to truncate file by lines?

I have a large number of file, some of which are very long. I would like to truncate them to a certain size if they are larger by removing the end of the file. But I only want to remove whole lines. ...
Charles's user avatar
  • 529
15 votes
2 answers
20k views

Write n bytes from a file into another in Bash

Hello how can I write n bytes from one file into a new file starting from the k position using Bash? For example if n=60, k=1 and file size=100 then: the 2nd file would consist of from the 1st byte ...
PiNewbie's user avatar
  • 151
14 votes
3 answers
5k views

why doesn't a bash while loop exit when piping to terminated subcommand?

Why doesn't the command below exit? Rather than exit, the loop runs indefinitely. While I discovered this behavior using a more complex setup, the simplest form of the command reduces to the ...
stephen.z's user avatar
  • 373
14 votes
2 answers
12k views

bash completion for filename patterns or directories

I'm trying to get a bash completion script set up and having some trouble. I would like to set it up so the completions listed are either files matching a particular extension, or directories (which ...
Rob I's user avatar
  • 243
13 votes
3 answers
27k views

Process each line of a file in bash

When I want to do something with each line in a file I usually write cat my_file | while read a do gzip "$a" done The gzip is just an example it can be anything. What I'm wondering is if there is ...
Nifle's user avatar
  • 34.5k
13 votes
3 answers
15k views

How get full path to target of link

If I have a symbolic link /var/opt/foo created with ln -fs /path/to/target/dir foo. How can I in a script that sees only the link get /path/to/target/dir? What I want to achieve in the script is rm -...
Nifle's user avatar
  • 34.5k
13 votes
6 answers
62k views

awk / sed to print only up to the underscore character

How can I use awk or sed to print a string only up to the first underscore character? Before: host100_044 2 host101_045 2 host102_046 2 After: host100 host101 host102
Rubstr's user avatar
  • 131
12 votes
2 answers
3k views

Difference when using backticks in double quotes and single quotes in Bash

Why to the following two executions differ in output? (I need double quotes for variables in my eventual command) $ sudo su -c "echo `cat /root/root_file`" cat: /root/root_file: Permission denied $ ...
Ambidex's user avatar
  • 385
12 votes
2 answers
20k views

Is there a way to pass arguments via pipe on bash?

Is there any way to pass arguments via pipe on bash? something like: echo value1 value2 | mkdir -p ./$1/$2 The line above would create the directories ./value1/value2 If this is not possible I ...
guest9586's user avatar
  • 133
12 votes
1 answer
11k views

Linux: How does file modification time affect directory modification time and directory access time?

I would like to learn more about how file access time and file modification time related to directory access and modification times. I did read the "questions allowed here" but if there is a better ...
Bulrush's user avatar
  • 335
11 votes
4 answers
14k views

how to properly use $1 in an alias with two arguments

i have to compare a number of files and I don't want to change the command in two places all the time. so i want to create an alias in bash. alias gd='gvimdiff $1 dir/$1' so that i can get ...
kirill_igum's user avatar
  • 1,318
11 votes
4 answers
4k views

How to properly restore settings when running a script using "source"?

I'm writing a script that needs to be executed using source, because its task is to modify the environment. During the script, I want to use set -e so that any error stops the script. The script looks ...
Petr's user avatar
  • 3,173
11 votes
2 answers
30k views

How do I concatenate strings from a list in a bash script?

As an example: List="A B C D" for I in $List do OUT=$OUT" -$I" done When I run this the result is: " -A -B -C -D" but want it to be: "-A -B -C -D" How do I concatenate without the leading ...
Seb S's user avatar
  • 213
11 votes
2 answers
20k views

How to get list of all child process spawned by a script

Context: Users provide me their custom scripts to run. These scripts can be of any sort like scripts to start multiple GUI programs, backend services. I have no control over how the scripts are ...
irraju's user avatar
  • 221
10 votes
3 answers
20k views

how to gzip and scp at the same time

I have a file in which i wanted to zip up and at the same time transfer over to another host using scp. I tried to do the following command but failed. I do not mind zipping up and scp over later, ...
Noob's user avatar
  • 1,625
10 votes
1 answer
3k views

Get bash to respect quotes when word splitting subshell output

I have a command that outputs some parameters that I want to pass to another command. However, when I run the command in a subshell, the output is subject to word splitting unless I quote the whole ...
Old Pro's user avatar
  • 2,493
10 votes
1 answer
12k views

How to change/add a tracker to a .torrent file using the command line?

I use transmission-daemon on Debian for my torrents. For now, I've been using the website http://torrenteditor.com/ to add trackers to .torrent files. I'd like to edit .torrent files directly using ...
bolino's user avatar
  • 309
9 votes
3 answers
55k views

Insert quote into string variable in bash

Is there way of inserting a quote at the start and end into a string variable? I have this variable say: string="desktop/first folder" If I echo this, it will display: desktop/first folder I need ...
Ali Gul's user avatar
  • 93
9 votes
1 answer
36k views

cp: cannot stat '/some/path/*': No such file or directory

My script code is: var="/some/path" cp "$var*" "/another/path" Then it throws: cp: cannot stat '/some/path/*': No such file or directory I have tried this and got the same error: cp "${var}*" "/...
syxbyi's user avatar
  • 93
9 votes
2 answers
17k views

Use "read" in Bash script with a standard answer

How can I use read with something as standard response, which the user can change though? (a default answer)
Flo's user avatar
  • 101
9 votes
2 answers
7k views

Which bash rc files are run on non-interactive/non-login shells?

My Understanding there are no rc files called for non-interactive/non-login shells; for example, those run by cron (I don't know this for certain and would lean on the community's expertise) non-...
vol7ron's user avatar
  • 485
9 votes
1 answer
10k views

Automated Interaction with Google Chrome

All right SU, I have a fun nut to crack. I have 8 screens set up in a line, all hooked into one beefy G5 running Snow Leopard (yes, 4 graphics cards; about 15,000 pixels wide!). I am setting up a ...
slifty's user avatar
  • 1,569
8 votes
3 answers
1k views

What commands would I use to create two child processes serially?

I want to spawn two child processes, but wait until the first reaches a certain point in its execution (which can be determined by examining stdout) before spawning the second. I'm not sure which unix ...
w.brian's user avatar
  • 205
8 votes
1 answer
3k views

How to make sed omit not matching lines?

In the following example, sed matches lines starting with an a or a c and prints the first character of that line (a or c): $ echo "ag bh ci dj ek fl" | sed 's/\(a\|c\)./\1/' # Matches lines starting ...
freitass's user avatar
  • 201
8 votes
3 answers
8k views

Bash: better way to output to both console and output file than tee?

What I need to display is a log refreshing periodically. It's a block of about 10 lines of text. I'm using |tee and it works right now. However, the performance is less satisfying. It waits a while ...
tmp's user avatar
  • 141
8 votes
1 answer
10k views

Bash script to toggle monitor input from HDMI to DisplayPort and vice-versa

I use ddcutil as a command line alternative to physically pressing the buttons on my monitor to change input from HDMI to DisplayPort, and have it initially set it up to switch from one to the other ...
Xixor's user avatar
  • 93
8 votes
3 answers
22k views

how can sed get patterns from a file

I have two files one holds a list of patterns, another need to be changed based on these patterns. So far I have been experimenting with cat patterns.txt| xargs -n1| sed 's/patternfromfile/...
sgp667's user avatar
  • 741
8 votes
5 answers
7k views

Delete all files (except the four newest) throughout directories using a Bash script

I'm looking for a Bash script that will go into a list of directories and delete all but the four most recently created files. How can I do this?
Marv1n's user avatar
  • 81
7 votes
1 answer
2k views

tmux - get pwd of another pane in a bash script?

Is there anyway to find out information about the current working directory of any pane from within a tmux session? I'd like to write a bash script that changes the directory of the current pane to ...
Brad Parks's user avatar
  • 3,056
7 votes
1 answer
23k views

Bash script: "[[: not found"

This is my code: #!/bin/bash if [[ -d ~/viwiki ]]; then cd ~/viwiki else mkdir ~/viwiki cd ~/viwiki fi if ! [[ -d ./log ]]; then mkdir log mkdir log/log mkdir "log/wget" elif ! [[ -d ./log/log ]...
Kiet Ho's user avatar
  • 95
7 votes
1 answer
5k views

Cannot resize tmux pane from bash script

Background and Problem: I have created a tmux script to open a few windows. It works great but then I wanted to place a vertically split pane on one window and resize it to 5 characters tall. I ...
Eric H's user avatar
  • 193
7 votes
5 answers
4k views

How to move all sibling folders into one folder?

I apologize that this seems like such a dumb question, but I can't afford to screw it up. I have a bunch of folders like so: /users/me/foo/oops1 /users/me/foo/oops2 /users/me/foo/oops3 /users/me/foo/...
jgritty's user avatar
  • 301
7 votes
2 answers
3k views

'while read' loop through lines in text file losing characters in Bash script. is FFmpeg line to blame?

I am looping through a text file, using 'while read'. I read three variables from each line - two filenames and a decimal number. I know that this works in a vanilla setup (loop, read line, echo line, ...
Dilgreen's user avatar
7 votes
3 answers
615 views

Why some commands don't take input redirection?

For example, echo: [aesteban@localhost ~]$ cat tmp.txt Angel [aesteban@localhost ~]$ echo < tmp.txt [aesteban@localhost ~]$ As you can see, the output is just a blank line. According to my ...
angelcool.net's user avatar

1
2 3 4 5
20