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

5
votes
1answer
94 views

Meaning of “:-” in bash [duplicate]

I have a bash code export TM_SCALAC=${TM_SCALAC:-scalac}. I'm not sure the meaning of ":-" inside the ${}. How do I interpret this line of bash code?
5
votes
3answers
2k views

How to make bash put prompt on a new line after cat command?

What I get: host:~ user$ cat example.txt some texthost:~ stas$ What I want to get: host:~ user$ cat example.txt some text host:~ stas$ Is there a way I can make cat behave like this? I'm using ...
5
votes
1answer
3k views

Trouble in script with spaces in filename

I've got a script that scp's a file from remote host back to local. Sometimes the file names contain spaces. scp does not like spaces in its file names. For some reason my attempts at handling the ...
5
votes
2answers
707 views

Disable mail check on login?

When I login to console, e.g tty1, I usually see a message No mail, I know there's some program checking email on login, but I don't use that, how can I disable that command ? I tried to grep from ...
5
votes
5answers
2k views

Unix bash script check empty gz files

I'm trying to write a bash script that will go into a directory loop through the .gz files and delete them if they are empty (ie the uncompressed file contained within in empty. I've got a couple of ...
5
votes
2answers
272 views

Display Non-Zero Return Status in PS1

I want to include the return status in my prompt. (Easy add '$? ', right?) However, I only want the status returned (and trailing space) if non-zero. Example: sd ~ $ false 1 sd ~ $ true sd ~ $
5
votes
2answers
5k views

loop curl get request bash

I'm trying to automate a curl script and eventually make it loop also. I found that I can't use any loop like: for i in `cat authors` do curl *line here* ...
4
votes
3answers
359 views

How can I pass argument stored on a variable to WGET

I'm writing a bash script that extensively uses wget. To define all common parameters in one place I store them on variables. Here's a piece of code: useragent='--user-agent="Mozilla/5.0 (Windows NT ...
4
votes
2answers
128 views

How to delete a directory automatically when an executable is killed

Quite often we run an executable that needs to write / read some temporary files. We usually create a temporary directory, run the executable there, and delete the directory when the script is done. ...
4
votes
3answers
2k views

Use a shell variable to execute a comand

I have a unix command in a variable, it looks like this: cmd="find /path/to/webpage -type f | grep -v .svn | xargs grep $@" `$cmd` find: paths must precede expression Usage: find [-H] [-L] [-P] ...
3
votes
2answers
121 views

Refer to an item in `dirs`

I use pushd to work with multiple directories in bash and zsh. I've aliased dirs to dirs -v so that I get an ordered list when I want to see what's on the directory stack: chb$ dirs 0 ...
3
votes
3answers
143 views

Persistent shell arguments

I often find myself running a series of commands on a single set of arguments. For example: $ nmap -Pn -sS 192.168.1.5 $ ssh 192.168.1.5 $ curl 192.168.1.5 $ wget -r 192.168.1.5 This can be sped ...
3
votes
3answers
2k views

Bash script that reads filenames from a pipe or from command line args?

I want my script to read a bunch of filenames (which may have spaces) given either as a glob or from STDIN and do stuff with them. I've been able to read either way separately, but not combine them. ...
3
votes
2answers
1k views

Insert a multiline string into another string

I need to insert lines into an xml file : Insert <one> </one> into <tags> </tags> To obtain <tags> <one> </one> </tags> I tried this: ...
3
votes
5answers
791 views

How can I test whether connection to the given host/port is established in Bash?

Currently I'm using netstat for this: if netstat -an | grep ESTABLISHED | grep $address:$port > /dev/null; then # command fi Is there a more elegant solution?
2
votes
4answers
620 views

Parsing process command-line arguments from pargs in a shell script

I get list of PIDs in my bash scripts (Java processes) and have to analyze their command-line arguments to determine which instance of server each PID corresponds to. At the moment I do it that way ...
2
votes
2answers
536 views

Detect if mouse button is pressed, then invoke a script or command

Is there any way to get mouse button state from bash? Xdotool can only get mouse location. I'd like to set something up where I can define a rule where when a mouse button is pressed, a script can ...
2
votes
2answers
529 views

xargs loop with input variable for use in multi-command

I have a text file with various IP addresses. I would like to cycle through each, and issue a command: cat ips.txt | xargs -L 1 `ssh me@__IP__ echo "text" > file; reboot;` But I'm unsure how ...
2
votes
2answers
689 views

Use of quotes in GNU grep regular expressions

I could see that certain patterns in the GNU Grep can be enclosed within brackets and certain others need not be. For example, matching the beginning of a word works only if it is enclosed within ...
2
votes
2answers
5k views

Bash/Korn shell script edited on Windows throws error '…^M: not found'

Some background on the issue: I am new to scripting and wrote a simple Bash script in Windows using Notepad++. cxStopAllServicesOnSERVER.sh #!/usr/bin/bash cd "/some/path" echo "Hi. Logs can be ...
2
votes
4answers
532 views

Stripping all vowels but the first from a set of strings

I have a string comprised of multiple substrings, separated by underscores. For example: AbcdAEfd_hEgdgE_AbAAAAA. I need to remove all vowels except the first from each substring. So: AbcdAEfd -> ...
2
votes
6answers
914 views

How to name a file in the deepest level of a directory tree

How do I name one random file in the deepest level of a directory tree using basic Bash commands and script? I was searching for a way to do it as an one-liner and without functions. Assuming also ...
1
vote
2answers
90 views

can you only input the password once and then execute the su command without entering a password? [duplicate]

I am copying files from a remote server to my local server. Here is the code that does this: sshpass -p "password" ssh -q username@$192.168.162.156 "mkdir -p /temp ; ...
1
vote
1answer
98 views

Recover my history of past commands?

Always when I turn on my Fedora 18, my history command disappear. How can I recovery all my history since I installed my OS? In my ./bash_history show olny the command of the day when I turn off ...
1
vote
1answer
36 views

bash completion on posix mode

I would like to use an interactive posix bash shell but the autocompletion feature doesn't work. When i type the tab character it simply prints a tab. The tab completion only works when i'm on a non ...
1
vote
4answers
118 views

regex matching with “locate”

I'm trying to use the locate command to find files in my home folder, however whenever I try and run this I get no results: locate -i -l 4 --regexp '^\/home\/jack\/[A-Za-z0-9\/\ ]*(My.)*$' I've ...
1
vote
4answers
817 views

How do I delete files with spaces in them in bash script?

this is a three part topic to a two part process that still is not working for me: Objective: to manipulate a filename then delete it the filename has spaces between the words then the dot .ext ...
1
vote
2answers
174 views

Why is variable interpolation not working for variable assignment (for bash)?

I have this script: ROOT=/tmp LOG=$ROOT/blah.log echo $LOG The output is: /blah.log obviously the ROOT variable is not being interpolated which is very unexpected. If I echo $ROOT, it correctly ...
1
vote
2answers
1k views

Trouble with read line script in Cygwin

I'm on windows 7 using Cygwin. My script and text file are located in the same directory. #!/bin/bash while read name; do echo "Name read from file - $name" done < /home/Matt/servers.txt I get ...
1
vote
3answers
786 views

Sort with unequal whitespace in first column

I need to sort this list by name, high temp and low temp: Kuala Lumpur 78 56 Seoul 85 66 Karachi 95 75 Tokyo 85 60 Lahore 85 75 Manila 90 85 I figured since whitespace is the delimiter for a column ...
1
vote
3answers
2k views

output only file names(with spaces) in ls -Al

I should echo only names of files or directories with this construction: ls -Al | while read string do ... done ls -Al output : drwxr-xr-x 12 s162103 studs 12 march 28 12:49 personal ...
1
vote
1answer
505 views

Is it possible to use a parameter within an alias command [duplicate]

Possible Duplicate: How to pass parameters to an alias? As answered in Can less retain colored output? I want to use git diff --color=always filename | less -r to get a colored output of my ...
1
vote
2answers
14k views

How to set environment variable JAVA_HOME that will be saved on exit? [duplicate]

Possible Duplicate: How do I set a user environment variable? (permanently, not session) I use Suse. First was installed jdk7, so JAVA_HOME remains like: /usr/java/jdk1.7.0_05/ After I ...
1
vote
1answer
17k views

How do I execute a remote shell script over SSH and be prompted for passwords by commands that require it in that script?

I want to do something like this ssh user@remote-domain.com ./remote_script.sh Contents of remote_script.sh #!/bin/bash hg pull This example is much simpler than what I am actually doing. I know ...
0
votes
1answer
134 views

How do you time how long a command took to run? [duplicate]

How would you find out how long a running process took to complete? Example: date; dd bs=1m if=/foo of=bar; date ^This example only has 1 second of resolution. Any shell is acceptable.
0
votes
3answers
270 views

Syntax errors in a bash script executed from tcsh

I wrote the line below in order to verify if the first or second fields of a list of numbers are equal to 146. I want to run the line from tcsh, with one line interpreted by bash. What's the problem ...
0
votes
2answers
193 views

Why does this compound command report errors when copying directories?

if one executes the following two commands in one line, as follows, rm -rf dir ; cp -r dir2 dir it may complain that cp can not create directory dir/subdir: File exists but if these two commands ...
0
votes
4answers
8k views

Is there a difference between sudo su - root and sudo -u root -H /bin/bash?

Like title says, is there a difference between these two commands : sudo su - root sudo -u root -H /bin/bash I'm using GNU/Linux, if that makes a difference.
-4
votes
0answers
45 views

“unexpected end of file” error in a bash script [closed]

#!/bin/bash APP_FILE="/tmp/name_file.txt" IPFILE="/tmp/ipaddres_file.txt" PINGDETAILS="/tmp/ping_results" NEWFILE="/tmp/new_file" echo "" > new_file echo "" > ping_results serverup_count=0 ...
60
votes
12answers
79k views

How can I resolve a hostname to an IP address in a Bash script?

What's the most concise way to resolve a hostname to an IP address in a Bash script? I'm using Arch Linux.
44
votes
6answers
43k views

How to add a newline to the end of a file?

Using version control systems I get annoyed at the noise when the diff says No newline at end of file. So I was wondering: How to add a newline at the end of a file to get rid off those messages?
25
votes
2answers
1k views

What does a “< <(…)” redirection mean?

I've seen that rvm (ruby version manager) is installed using the following command: bash < <(curl -s https://raw.github.com/wayneeseguin/rvm/master/binscripts/rvm-installer ) So as I ...
31
votes
6answers
23k views

Why doesn't my Bash script recognize aliases?

In my ~/.bashrc file reside two definitions: commandA, which is an alias to a longer path commandB, which is an alias to a Bash script I want to process the same file with these two commands, so I ...
21
votes
3answers
55k views

What is the “eval” command in bash?

What can a man do with the "eval" command, why is it usefull? It's some kind of built-in function in bash? There is no "man" page for it..
34
votes
3answers
11k 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 ...
23
votes
3answers
3k views

What is the purpose of the hash command?

If you run hash it shows the path of all commands run since the hash was last reset (hash -r) [root@c04c ~]# hash hash: hash table empty [root@c04c ~]# whoami root [root@c04c ~]# hash hits ...
9
votes
4answers
5k 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
1answer
3k views

Terminal autocomplete: cycle through suggestions

I had this on my Ubuntu setup and since I switched to Fedora I want to set it and I forgot how... The idea is simple : I don't want the terminal to show me suggestions when I double tab, instead I ...
23
votes
8answers
32k views

Is there a way to get the min, max, median, and average of a list of numbers in a single command?

I have a list of numbers in a file, one per line. How can I get the minimum, maximum, median and average values? I want to use the results in a bash script. Although my immediate situation is for ...
19
votes
3answers
7k 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 ...