Questions about bash only, as opposed to other Bourne/POSIX shells. For all Bourne/POSIX-style shells, use the /shell tag instead.

learn more… | top users | synonyms

1
vote
1answer
742 views

Splitting stdin to different outputs in bash [duplicate]

Possible Duplicate: Is there a way in bash to redirect output and still have it go to stdout? How would I send /dev/stdin to both /dev/stdout and a $logfile in one command? One solution ...
2
votes
3answers
4k views

Extracting tokens from a line of text

Using bash scripting and grep/awk/sed, how can I split a line matching a known pattern with a single character delimiter into an array, e.g. convert token1;token2;token3;token4 into a[0] = token1 … ...
4
votes
4answers
750 views

How to make functions created in a bash script persist like those in .bashrc?

My .bashrc was getting a little long, so I decided to break it up into smaller files according to topic and then call these files from within .bashrc as so #my long .bashrc file bash .topic1rc bash ...
8
votes
2answers
903 views

How can I use $var in a shell brace expansion of a sequence?

I want to use $var in a shell brace expansion with a range, in bash. Simply putting {$var1..$var2} doesn't work, so I went "lateral"... The following works, but it's a bit kludgey. # remove the ...
6
votes
4answers
3k views

Copy a File From One Zip to Another?

I have a file named 'sourceZip.zip' This file ('sourceZip.zip') contains two files: 'textFile.txt' 'binFile.bin' I also have a file named 'targetZip.zip' This file ('targetZip.zip') contains ...
5
votes
2answers
1k views

How does bash interpret the equal operator with no surrounding spaces in a conditional?

The following script does not behave as I would have expected. Adding spaces around the '=' in the conditional made it perform how I wanted, but it got me thinking, what is it actually doing inside ...
4
votes
1answer
494 views

Difference between ' and " on command line (bash)?

I used to use '' and "" interchangeably on the command line, but I recently noticed that '$HOME/some/dir' is not expanded, while "$HOME/some/dir" is. I searched around a little bit and found that "" ...
28
votes
7answers
43k views

Execute a command once per line of piped input?

I want to run a java command once for every match of ls | grep pattern -. In this case, I think I could do find pattern -exec java MyProg '{}' \; but I'm curious about the general case - is there an ...
3
votes
2answers
1k views

I no longer have smart bash completion for normal user

I created a new user on our Ubuntu server and I no longer have the bash auto/smart completion (pressing tab once finishes your current phrase if possible. Two tabs give you a ls). Smart complete ...
2
votes
4answers
1k views

rename directory of images numeric

I'm trying to rename a directory full of images like so: test.jpg funny.gif sad.png filename.PNG filenaaskjas.JPG to: 1.jpg 2.gif 3.png 4.png 5.jpg I've got it working ...
5
votes
1answer
1k views

Bash script IDE

Is there a bash/ksh/any shell script IDE. Don't you get annoyed when you forget the space inside if or I don't know, some minor syntax mistakes you do from time to time, but takes you a long time to ...
4
votes
2answers
419 views

Jump to start of the line in Bash's Vi mode

I'm using Bash Vi mode (aka set -o vi). Still I miss the Ctrl-A shortcut from Emacs mode, which is very handy. Many times I'm retrieving the last command and append an echo to the beginning of the ...
4
votes
1answer
210 views

terminal - having “$” instead of MyComputer:~ me$

Right now in my MacOS X terminal I have MyComputer:~ me$ before all the commands. In some videos I saw they have just $ - that's much less clutter, how is it done?
5
votes
2answers
2k views

Shell script execution on multiple servers

Is it possible to run one part of the ksh/sh script on one server then ssh to another server and continue with the rest of the script? or is there a way around? Anyone had experience with this ? here ...
5
votes
1answer
3k views

Bash flush standard input before a read

Is there an easy way in bash to flush out the standard input? I have a script that is commonly run, and at one point in the script read is used to get input from the user. The problem is that most ...
2
votes
2answers
735 views

grep acting strangely

Grep has been acting strangely on one of my systems (Ubuntu Desktop - all my other systems are Ubuntu Server), and I can't figure out why. I created a control file named text that contains the ...
2
votes
2answers
107 views

How to get history search to start afresh?

When I do a search using CtrlR, the search gets limited to entries older than displayed hit. How do I reset this so that it starts from the beginning (i.e. from latest history entry) without leaving ...
5
votes
1answer
1k views

How to search the whole of bash history without needing to go forwards and backwards?

At times when I do CtrlR to search through command history, I find that I don't reach the entry I was looking for, even though I know it's in history. I think this is due to me having passed the ...
11
votes
4answers
7k views

How can I rename photos, given the EXIF data?

Let's say I have a bunch of photos, all with correct EXIF information, and the photos are randomly named (because of a problem I had). I have a little program called jhead which gives me the below ...
5
votes
3answers
443 views

How do I delete a file named “°” in bash

I've accidentally created a file named °. Now I'm having trouble deleting it with bash. [/opt/etc/sudoers.d] # ls -l -r--r----- 1 admin administ 21 Feb 3 23:54 010-root -rw-r--r-- 1 ...
7
votes
3answers
4k views

Bash won't execute files

I have a foo.sh file in my current directory. If I try to run ./foo.sh, I get: -bash: ./foo.sh: /bin/sh: bad interpreter: Permission denied But if I run /bin/sh ./foo.sh then it runs fine. How can ...
5
votes
5answers
2k views

Changing the PS1 on an interactive bash subshell easily

I have a situation where I need to provide a subshell to a user mid-way through a longish process. I would like to change the prompt to remind the user that they are in a special subshell and haven't ...
7
votes
4answers
2k views

How can I check if a gzipped file is empty?

Is there a quick way to check if a gzipped file is empty, or do I have to unzip it first? example: $ touch foo $ if [ -s foo ]; then echo not empty; fi $ gzip foo $ if [ -s foo.gz ]; then echo not ...
9
votes
7answers
7k views

How do I suppress dd output?

I have a bash script that creates some file using dd. The problem is dd throws a great amount of output that is going to mess with the output of my script. Searching around I've found a solution: dd ...
2
votes
2answers
268 views

Perl find and replace is writing every file

Here's the test script I'm running matt@server:~ $ cat test.sh #!/bin/bash mkdir test cd test echo "has the string foo" > yes.txt echo "hasn't the string" > no.txt ls -l --time-style=full-iso ...
4
votes
2answers
1k views

using brace expansion to change filenames, not extensions?

I have a directory containing a bunch of files with different extensions, but the filename before the extension is all the same. E.g., file1.txt, file1.jpg, file1.pdf, file1.odt... I want to change ...
8
votes
4answers
9k views

Number of files containing a given string

How can I count the number of files (in a directory) containing a given string as input in bash/sh?
6
votes
4answers
13k views

Processing bash variable with sed

Been banging my head off a wall on this bash variable LATLNG contains a latitude & longitude value in brackets like so (53.3096,-6.28396) I want to parse these into a variable called LAT and ...
8
votes
5answers
2k views

How to run part of a script with reduced privileges?

I have the following problem: On every machine running Postgresql there is a special user postgres. This user has administrative access to the database server. Now I want to write a Bash script that ...
3
votes
2answers
410 views

Where is the setting that disallows duplicate command history?

Let's say I type the following sequence of commands: $ cd $ ls $ ls When I go back in history by pressing up twice, I end up with cd, instead of ls. I've noticed this isn't always the case, so it ...
3
votes
1answer
759 views

Storing `find` parameters in a variable

I'm running the following bash command: find . \( -iname '*.aif' -o -iname '*.pdf' -o -iname '*.exe' -o -iname '*.mov' \ -o -iname '*.doc' \) -exec rm -f {} \; I'm running the same parameters ...
8
votes
2answers
11k views

Excluding a directory when zipping files

I've got a bash script that does something like this: zip -0 ../backup/backup.zip \ -r ./* \ -x \*CVS\* \ -x *Thumbs.db* \ The directory it's backing up is a SVN archive (it used to be CVS back in ...
11
votes
2answers
6k views

Why would anyone not set 'histappend' in bash?

After finding out what this shopt -s histappend means, it seems a very sane setting, and I'm surprised that it isn't default. Why would anyone want to wipe their history on each shell exit?
2
votes
2answers
5k views

Using an incrementing variable in a bash command line for loop?

I'm using a bash commandline for-loop to concat a group of files together, and I'd like to append an incrementing digit. Something like this: j=1;for i in *.bak; do echo {$j++: >> ...
11
votes
7answers
8k views

How to check if $PWD is a subdirectory of a given path

E.g. check if $PWD is a subdirectory of /home. In other words I'm searching for a bash string operation to check if one string starts with another.
16
votes
4answers
8k views

How to redirect stderr,out to different files and also display in terminal?

To make it clear, I want to see the output of a command in the terminal as if there were no redirection. Also stderr need to be redirected to err.log and stdout need to be redirected to stdout.log. ...
8
votes
4answers
1k views

Can I easily search my history across many screen windows?

My current screen session has 12 open windows on it. It's been running for weeks... I know I executed an ImageMagick convert command in one of these 12 screen windows sometime last week... is there ...
2
votes
1answer
1k views

ant script stops, waiting for input when run in background

I'm running an ant (Java build tool) script on CentOS 5.5 that execs another java process. When I run the ant script in the background: ant -f myfile.xml &> foo.out & The forked process' ...
38
votes
4answers
5k views

Is there any way to keep a command from being added to your history?

Is there any way to keep a command from being added to your history? I have a command that I want to keep out of my history file, and I really don't care to have it there when I search the history ...
5
votes
2answers
5k views

How to combine bash command grouping and pipe status

How do I combine bash command grouping and pipe status? This is an example command group: { tar -cf - my_folder 2>&1 1>&3 | grep -v "Removing leading" 1>&2; } 3>&1 | gzip ...
30
votes
6answers
3k views

When do you use brace expansion?

I understand what brace expansion is, but I don't know how best to use it. When do you use it? Please teach me some convenient and remarkable examples if you have your own tip.
5
votes
2answers
13k views

Get the free space available in current directory in Bash

I know df -h and pwd, but it seems a little complex for the regex matching part. Any ideas?
3
votes
2answers
1k views

automatically load shell scripts from /usr/bin

Is there a way to have bash automatically load shell scripts I create and place into /usr/bin? Or is there a different place I should put custom shell scripts so that they are automatically loaded? ...
7
votes
2answers
3k views

Iterate over the output of a command in bash without a subshell

I want to loop over the output of a command without creating a sub-shell or using a temporary file. The initial version of of my script looked like this, but this doesn't work since it creates a ...
12
votes
7answers
2k views

Open a file given by the result of a command in vim

I find myself doing the following almost every day Run a find (find -name somefile.txt) Open the result in vim The problem is I have to copy and paste the result of the find into the vim command. ...
8
votes
2answers
7k views

Adding an empty line at the end of input

I have some command which produces output with no new line at the end, like this Myprompt$ somecmd dksfjdl dsfjdlkfj dsfjdkfj dfjdkfjMyprompt$ Currently I overcome this by somecmd | sed 's/$/\n/' | ...
13
votes
3answers
21k views

Bash script to find and kill a process with certain arguments?

I want a script which kills the instance(s) of ssh which are run with the -D argument (setting up a local proxy). Manually, I do ps -A | grep -i ssh, look for the instance(s) with -D, and kill -9 ...
5
votes
3answers
7k views

History command inside bash script

I have been bashing my head to write a simple history script for the last two days. History is a shell-built in command I couldn't able to use that within a BASH script. So, Is there a way attain this ...
97
votes
4answers
22k views

How do I clear Bash's cache of paths to executables?

When I execute a program without specifying the full path to the executable, and Bash must search the directories in $PATH to find the binary, it seems that Bash remembers the path in some sort of ...
2
votes
4answers
2k views

Shell script error message

I am trying to learn shell scripting by reading linuxcommand.org. For some reason I keep getting the error "Not Found", even though it runs all the commands. The error codes I get are: ...