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

10
votes
7answers
4k views

How can I display the contents of a text file on the command line?

I would like to display the contents of a text file on the command line. The file only contains 5-6 characters. Is there an easy way to do this?
1
vote
0answers
10 views

When using expect: SSHFS Transport endpoint is not connected

When I run my SSHFS connection script from bash, everything works perfectly. However, when I run the same script using the expect utility, I get the following error when I try to access the folder ...
42
votes
13answers
27k views

How can I get my external IP address in bash?

I need to find my external IP address from a bash script. At the moment I use this function: myip () { lwp-request -o text checkip.dyndns.org | awk '{ print $NF }' } But it depends on ...
1
vote
1answer
19 views

split a file, pass each piece as a param to a script, run each script in parallel

I have a words.txt with 10000 words (one to a line). I have 5,000 documents. I want to see which documents contain which of those words (with a regex pattern around the word). I have a script.sh that ...
6
votes
4answers
279 views

How can I test if a variable is empty or contains only spaces?

The following bash syntax verifies if param isn't empty: [[ ! -z $param ]] For example: param="" [[ ! -z $param ]] && echo "I am not zero" No output and its fine. But when param ...
1
vote
3answers
34 views

Command to 're-apply' previous command?

Is there an easy way to re-apply a previous command to a new command line entry? Say I typed in chmod u+r,g+x file.txt but forgot the sudo. Could I simply type sudo <some easy symbol>'? ...
2
votes
1answer
21 views

Is using shorthand quick substitution of history expansion problematic?

In my terminal (bash 3), I sometimes use the quick substitution ^aa^bb^ ^string1^string2^ Quick substitution. Repeat the last command, replacing string1 with string2. Equivalent to ...
1
vote
1answer
13 views

X11 Issues running with Terminal.app

I am trying to figure out why I can't use ghostscript or ghostview (or any other X11 applications) when I try to call them in the Terminal. I am using bash, and I also used Fink to get the packages I ...
2
votes
3answers
39 views

moving a (file | directory) while avoiding filename collisions

I have a bash script that moves files from a number of different locations to a folder named completed. I want to avoid overwriting previous files, so in the case when the name of a file (for ...
0
votes
1answer
12 views

For a given directory, how do I concatenate the tail end of recently modified files to a new file?

In case there's a better solution to the problem, my goal is to be able to quickly analyze ALL recent apache activity in /usr/local/apache/domlogs. The usefulness of this is when the server load is ...
7
votes
3answers
13k views

How do I count the number of occurrences of a word in a text file with the command line?

I have a large JSON file that is on one line, and I want to use the command line to be able to count the number of occurrences of a word in the file. How can I do that?
-1
votes
0answers
32 views

How to I create a file with a sorted list from users that have mail

I need to create a file ~/you have mail, it needs to contain a sorted list of all users on my local system which currently have mail in their "Inboxes" (i.n., their spool of delivered but no yet read ...
1
vote
1answer
238 views

How to see interactive output when running nmap from a bash script?

I'm trying to run nmap from a Bash script, in a way so that when Enter is pressed it should give some output, as happens when running directly from the commandline. When calling a few different nmap ...
14
votes
1answer
651 views

How can I `alias sudo !!`?

I'm trying to set an alias for sudo !! in Bash. I tried alias sbb='sudo !!', but it interprets that as a literal !! and prints sudo: !!: command not found If I use double quotes, it substitutes ...
5
votes
2answers
201 views

Why does !! inside an alias not work?

I have this alias set in my system /etc/bashrc file: alias root="sudo !!" The intention of this being to run the last used command using sudo of course. When used, it of course appears to ...
1
vote
2answers
86 views

Chown permission denied on owned dir

I have following directory (/usr/local) rwxr-xr-x. 43 root root 4096 Jul 25 14:19 . rwxr-xr-x. 14 root root 4096 Feb 21 10:27 .. rwxr-xr-x. 2 root root 4096 Jun 28 2011 bin [...] rwxr-xr-x 1 ...
1
vote
0answers
13 views

Using X11 Displays through Terminal.app commands : generic mapping tools

I am trying to figure out why I can't use ghostscript or ghostview (or any other X11 applications) when I try to call them in the Terminal. I am using bash, and I also used Fink to get the packages I ...
12
votes
5answers
4k views

How can I get bash to exit on backtick failure in a similar way to pipefail?

So I like to harden my bash scripts wherever I can (and when not able to delegate to a language like Python/Ruby) to ensure errors do not go uncaught. In that vein I have a strict.sh, which contains ...
0
votes
0answers
32 views

send email only when find finds something [on hold]

I would like to send an email using mutt, whenever this find command finds something, find . /home/user/todel -ctime +30 -type f -ls | mutt … At present it sends an email every time it is run. How do ...
2
votes
3answers
75 views

Count files in a directory by extension

For the purpose of testing, I'd like count how many images files are inside a directory, separating each image file type by file extension (jpg="yes". This because later it will be useful for another ...
2
votes
1answer
42 views

Forward SIGTERM to child in Bash

I have a Bash script, which looks similar to this: #!/bin/bash echo "Doing some initial work...."; /bin/start/main/server --nodaemon Now if the bash shell running the script receives a SIGTERM ...
1
vote
1answer
30 views

processing multiple extensions

I have done a script to convert recursive .jpg files to another size: echo $re mkdir "$re"_tmp for a in *.jpg ; do convert "$a" -resize $re "$re""_tmp/${a%.*} ["$re"].jpg" ; done I'd like integrate ...
0
votes
2answers
33 views

How to list similar lines?

The delimiters could be: " "; "_" INPUT: foo#foo_ehh113#The password of user 111 will expire within the next seven d foo#foo_ehh204#The password of user 111 will expire within the next seven d ...
1
vote
3answers
54 views

Creating an alias for a bash script

So I have tried experimented and created an alias in .bashrc. However, when I test out the command I get: [rkahil@netmon3 ~]$ menu -bash: menu: command not found Here is what I have in the .bashrc ...
2
votes
1answer
31 views

Ctrl-R style interactive search in custom file

I want to be able to search a custom text file similar to the interactive history search in bash. Once the wanted entry is found, executes the line. Bonus Points: have each record contain two ...
3
votes
3answers
75 views

why bash increment: `n=0;((n++));` return error? [duplicate]

n=0; ((n++));echo "ret=$?;n=$n;" ((n++));echo "ret=$?;n=$n;" ((n++));echo "ret=$?;n=$n;" from n=1 on, ((n++)) works correctly, only when n=0, ((n++)) return error, and I am using a trap '' ERR that ...
8
votes
5answers
13k views

How to stop the loop bash script in terminal?

For example, #!/bin/bash while : do sl done How to terminate this bash script?
3
votes
3answers
49 views

Is it possible to have a bashrc for a usergroup?

Is it possible to execute some bashrc file for all users in a group? If you can do that system-wide in /etc/profile then why not for a usergroup?
4
votes
1answer
32 views

Is it possible for bash completion to cycle through the alternatives?

When using bash completion and an a number of characters have been entered, tabbing ceases to work then the prefix you have typed is matched by more than one of the possibilities. Is there a way to ...
1
vote
1answer
56 views

Resolve nested aliases to their source commands

I've made several aliases for my convenience.But now I need to be helpful by sending a useful command and it is full of aliases.I've tried doing type u but all it returns is up && ap upgrade ...
16
votes
2answers
645 views

Does ~ always equal $HOME

I know this has probably been asked before, but I couldn't find it with Google. Given Linux Kernel No configurations that change $HOME bash Will ~ == $HOME be true?
4
votes
0answers
81 views
+50

Bash inside Vim not working properly

In MacVim I'm using Conque to run bash commands inside Vim. I have a problem with the text navigation while using Conque. If I'm at the end of a line of text and press down Alt← I skip a word to the ...
9
votes
3answers
76 views

Bash autocomplete with vim command

In bash, autocompletion of paths has recently stopped working when issuing vim commands where the path is deeper than two directories (it continues to work as expected with other commands, such as ls ...
2
votes
1answer
31 views

How can I make mailx inform me of new mail?

I'm trying to set up mailx on my server. On one server (Raspbian) I am informed of new mail with a message You have new mail (or something similar). On another machine (Raspbmc), I never have this ...
2
votes
2answers
51 views

Meaning of backslash

Why the following command prints Smith but not \Smith? echo \Smith
2
votes
2answers
65 views

How to write a function in bash?

I am drawing blank here. How should I have written this script? #!/bin/sh XZY_NODES="\ vl-ocnt-1 \ vl-ocnt-2 \ vl-onet-1 \ vl-onet-2 \ vl-ocmp-1 \ vl-oomp-2 \ " is_xyz_node () { host=$1 ...
4
votes
1answer
100 views

Proper way to distribute shell scripts

What is the most proper way to distribute shell scripts, if behaviours of shells can be modified by set and thus are unpredictable? For example, rm *.txt wouldn't be executed as expected on the ...
14
votes
3answers
794 views

Where is bash's history stored?

If I run history, I can see my latest executed commands. But if I do tail -f $HISTFILE or tail -f ~/.bash_history, they do not get listed. Does the file get locked, is there a temporary location or ...
4
votes
4answers
151 views

Indirection on a Bash array

Is there a possibility to write the following script without the loop? IPv4_first=1.1.1.1 IPv4_second=2.2.2.2 IPv4_third=3.3.3.3 IPv4_all=() for var in ${!IPv4_@} do IPv4_all+=(${!var}) done ...
-1
votes
0answers
45 views

Unique id for combination of Latitude - Longitude

I have a LibreOffice Calc table containing one column with latitude and one with longitude. What I want to do is for every unique combination of latitude and longitude a unique number to be assigned ...
13
votes
4answers
3k views

how to download a file using just bash and nothing else (no curl, wget, perl, etc.)

I have a minimal headless *nix which does not have any command line utilities for downloading files (e.g. no curl, wget, etc). I only have bash. How can I download a file? Ideally, I would like a ...
2
votes
2answers
75 views

How to clear the input in Bash

I have a bash script that is reading quite a bit of data /dev/random. I need the user to type randomly to seed the random daemon. However once the script completes everything typed will get dumped to ...
0
votes
3answers
4k views

Yum update problems on CentOS 6.5 minimal as VMware guest

INFO: I am using Windows 7 Pro (64bit) with VMware Workstation version 10.0.1 . I am using CentOS(64bit) 6.5 minimal, for the first time. I am running this from my workplace network, which may use a ...
1
vote
1answer
21 views

dialog --inputbox

Follow the below script I've a unique variable that keeps all variables, I need have each variable separated from $VALUES: #!/bin/bash shell="" groups="" user="" home="" exec 3>&1 ...
5
votes
2answers
67 views

“command | less” vs “less <(command)”

A common "idiom" for viewing large amounts of command output is to pipe it to less, via command | less. However, it's also possible (perhaps only in bash, haven't tested in other shells) to use the ...
1
vote
1answer
43 views

Memory buffer in a bash script

I have recently finished a script that will ask for a hostname and automatically take the argument of the function and ssh into one of my work servers. Here is a copy of my script: #!/bin/bash echo ...
7
votes
4answers
239 views

How can I suppress the space between generated arguments during brace expansion?

I used the following command to create a list of URLs that I want to test: echo -e ...
6
votes
2answers
418 views

Why is `tac file | grep foo' (piping) faster than `grep foo < <(tac file)' (process substitution)?

This question was motivated by "Reverse grepping", about grepping a huge file from bottom up. @chaos said: tac file | grep whatever Or a bit more effective: grep whatever < <(tac file) ...
3
votes
0answers
18 views

Errtrace and local

I am using the following script: #!/bin/bash -Eu trap 'echo Hi' ERR exit_failure() { echo "Hello, World!" return 1 } sub_failure() { res=$(exit_failure) } sub_failure It results in the ...
3
votes
2answers
29 views

bash 'nounset' on sub-shell/child doesn't force main script to exit, how to workaround that globally?

With this script: tstNounset.sh #!/bin/bash set -x set -o nounset;set -u echo `echo $str` function FUNC1() { echo $str; };export -f FUNC1;bash -c FUNC1 function FUNC2() { set -u;echo $str; };export ...