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

2
votes
2answers
435 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
386 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
642 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
6answers
860 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
76 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
32 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
2answers
155 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
714 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
404 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
4answers
514 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 -> ...
1
vote
2answers
13k 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
128 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
264 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
188 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
7k 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.
58
votes
12answers
75k 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.
41
votes
6answers
40k 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
21k 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 ...
20
votes
3answers
52k 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..
32
votes
3answers
10k 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 ...
8
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. ...
17
votes
2answers
3k views

aliasing cd to pushd - is it a good idea?

Is it a good idea to use the following alias: cd() { pushd $1; } in bash? I think this would be very useful, since I can then use a series of popds instead of just a cd - once. Is there any ...
15
votes
2answers
495 views

How can I stream data to a program that expects to read data from a file that is given as a argument?

I have a huge gzipped file and I want a program (4s-import in this case) to read it. It takes a lot of time to first unzip the file and then call the program with the path to the file as an argument. ...
8
votes
1answer
2k 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 ...
22
votes
8answers
30k 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
6k 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 ...
11
votes
2answers
509 views

Can the empty spaces/background in a terminal be replaced with a random(but pretty) pattern of ASCII characters?

Context and Question There are many ways to colorize the terminal and shell environment. The output of individual commands, such as ls and grep, can also be colorized. Not directly related but ...
19
votes
2answers
8k views

What is the difference between ~/.profile, ~/.bashrc, ~/.bash_profile, ~/.gnomerc, /etc/bash_bashrc, /etc/screenrc …?

Answers to the questions on SO and askubuntu, along with poking through (and reading headers of) $HOME and /etc/, indicate a number of files that can be used to set environment variables, including: ...
18
votes
3answers
6k views

Duplicate entries in $PATH a problem?

I source bashrc's of few of my friends. So I end up having duplicate entries in my $PATH variable. I am not sure if that is the problem for commands taking long to start. How does $PATH internally ...
16
votes
2answers
7k views

What does ${1+“$@”} mean in a shell script, and how does it differ from “$@”?

In the Perl documentation, perlrun(1) suggests launching Perl scripts using a bilingual shell/Perl header: #!/bin/sh #! -*-perl-*- eval 'exec perl -x -wS $0 ${1+"$@"}' if 0; What does ${1+"$@"} ...
15
votes
4answers
11k views

How to copy some, but not all files?

So, you can use the * as a wild card for all files when using cp within context of a directory. Is there a way to copy all files except x file?
20
votes
3answers
2k views

How does TAB auto-complete find options to complete?

And how does it also autocomplete aliases?
17
votes
3answers
1k views

Set and Shopt - Why Two?

set and shopt are both shell builtins that control various options. I often forget which options are set by which command, and occasionally which option sets/unsets (set +o/-o, shopt -s/-u). Why are ...
15
votes
4answers
5k views

Bash history: “ignoredups” and “erasedups” setting conflict with common history across sessions

First of all, this is not a duplicate of any existing threads on SE. I have read these two threads (1st, 2nd) on better bash history, but none of the answers work - - I am on Fedora 15 by the way. I ...
12
votes
2answers
1k views

How to safely pass variables to root-enabled scripts?

This question is totally general and not only applicable to my situation, but... I have a small busybox appliance where I want a non-root user to be able to execute a particular script with root ...
8
votes
4answers
11k views

Is there a way to set the size of the history list in bash to more than 5000 lines?

No matter how much I set the HISTSIZE environment variable to be larger than 5000, when printing the history list with the history builtin, it prints only the last 5000 commands. I need that because I ...
7
votes
4answers
3k views

How to make sure only one instance of a bash script runs?

A solution that does not require additional tools would be prefered.
4
votes
3answers
2k views

Best practice to use $? in bash?

When I read this answer about $? another question comes to mind. Is there any best practice for how to use $? in bash? Let's have a example: We have a linear script and I we would like to know ...
25
votes
3answers
2k views

Why shouldn't someone use passwords in the command line?

Why do people fear writing passwords in the command line? The history file is located in ~/.history, so it's available only to the user who executed the commands (and root).
17
votes
3answers
3k views

Bash: logical operators precedence &&, ||

I am trying to understand how the logical operator precedence works in bash. For example, I would have expected, that the following command does not echo anything. true || echo aaa && echo ...
16
votes
2answers
626 views

Quoting within $(command substitution) in Bash

In my Bash environment I use variables containing spaces, and I use these variables within command substitution. Unfortunately I cannot find the answer on SE. What is the correct way to quote my ...
16
votes
6answers
19k views

How do I can get the size of a file in a bash script?

How do I can get the size of a file in a bash script? How do I assign this to a bash variable so I can use it later?
14
votes
2answers
1k views

Can I configure bash to execute “clear” before every command typed in the console?

I would like to configure bash to execute clear command every time I type some command in the terminal (before executing my command). How can I do that? I'm using Debian Linux.
11
votes
2answers
956 views

find and remove duplicates in a directory

I have a directory with multiple img files and some of them are identical but they all have different names. I need to remove duplicates but with no external tools only with a bash script. I'm a ...
10
votes
2answers
526 views

Highlight the three last updated files in ls output

Is there any way to overload or wrap the ls command so that it will highlight / underline / otherwise make obvious the last three modified files? I know that I can simply ls -rtl to order by reverse ...