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
2answers
251 views

Setting PATH vs. exporting PATH in ~/.bash_profile [duplicate]

What's the difference and which is better to use when customizing my bash profile? Documentation on the export command is scarce, as it's a builtin cmd. Excerpt from version 1 of my ~/.bash_profile: ...
1
vote
2answers
174 views

Changing user to root when connected to a linux server and copying files

My script is coded in a way that doesn't allow you to connect to a server directly by root. This code basically copies files a server to my computer and it works but I don't have access to many files ...
10
votes
4answers
8k views

Command substitution: splitting on newline but not space

I know I can solve this problem several ways, but I'm wondering if there is a way to do it using only bash built-ins, and if not, what is the most efficient way to do it. I have a file with contents ...
8
votes
3answers
443 views

Running an executable in PATH with the same name as an existing function

Sometimes I define a function that shadows an executable and tweaks its arguments or output. So the function has the same name as the executable, and I need a way how to run the executable from the ...
8
votes
3answers
2k views

Is there a maximum size to the bash history file?

Can I increase the size of the command history in bash? Note that I use a Red Hat Linux computer in the undergraduate astrophysics department here (so I don't get that many privileges).
8
votes
3answers
2k views

Change the PS1 color based on the background color?

I learned that I can change the format of PS1, especially the color of the string. Then, is it possible to change the color based on the background color of the shell? Or, how can I detect the ...
7
votes
1answer
2k views

Non-blocking bash command

Let's say I have a bash script with the following: #!/bin/sh gedit rm *.temp When I execute it using sh ./test.sh, gedit pops-up but the rm part does not run until after I close gedit. I want the ...
6
votes
1answer
435 views

Why does this PS1 value cause unexpected behaviour? [duplicate]

Possible Duplicate: Why is my bash prompt getting bugged when I browse the history? I have set my PS1 variable in .bashrc to be the following: PS1='\e[35m[\W]:\e[0m ' It achieved what it ...
6
votes
2answers
2k views

Reading passwords without showing on screen in Bash Scripts

How to read passwords in bash scripts in a way like tools which are not showing it on terminal ? (Changing font to black on black might be tricky by easy to copy&paste - so it's not solution)
6
votes
1answer
651 views

Why does a=0; let a++ return exit code 1?

Try it: $ a=0 $ let a++ $ echo $? 1 # Did the world just go mad? $ echo $a 1 # Yes, it did. $ let a++ $ echo $? 0 # We have normality. $ echo $a 2 Contrast with this: $ b=0 $ let b+=1 $ echo $? 0 ...
5
votes
3answers
438 views

What are the color codes that I can use in my PS1 prompt?

I used several colors in my PS1 prompt such as \033]01;31\] # pink \033]00m\] # white \033]01;36\] # bold green \033]02;36\] # green \033]01;34\] # blue \033]01;33\] # bold yellow Where can I ...
5
votes
1answer
96 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
1answer
118 views

Change the location of .bashrc

Is it possible to change the location of .bashrc from /home/orhanc/.bashrc to some other directory?
5
votes
4answers
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
744 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
3answers
2k views

Colored FIND output?

Is it possible to get colored output from the find command? That is, in the path of each found item, directories are blue, executable scripts are green, et cetera? I'm using version 4.4.2 of GNU ...
5
votes
2answers
275 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
6k 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
1answer
114 views

`seq` and bash brace expansion failing

IINM my system is failing when bashing for i in {0..10000000}; # Seven zeroes. do false; done # `bash` exited and its `tmux` pane/window was closed. or for i in $(seq 0 10000000); # ...
4
votes
1answer
86 views

How do I stop a bash shell PS1 color to stop at the end of the command?

So simple question really, I have a custom PS1 colour where I have the actual shell commands in a distinct colour, just so I can quickly see what commands I typed and separate it from the command ...
4
votes
3answers
478 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
137 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
3k 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
140 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
146 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
384 views

Are there any utilities to quickly add, list and remove command aliases?

I'd like to be able to quickly, on-the-fly add (and list/remove) command aliases so that they persist between shell restarts (as if they were added to .bashrc). Are there any ready ...
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
814 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
967 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
689 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
673 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
738 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
6k 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
543 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
951 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
104 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
126 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
41 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
138 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
891 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
187 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
855 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
3k 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
596 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
15k 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 ...