Questions specific to GNU’s Bourne Again SHell, as opposed to other Bourne/POSIX shells. For questions about Unix shells in general, use the /shell tag instead.

learn more… | top users | synonyms

7
votes
3answers
12k views

Use config file for my shell script

I need to create a config file for my own script: here an example: script: #!/bin/bash source /home/myuser/test/config echo "Name=$nam" >&2 echo "Surname=$sur" >&2 Content of /home/...
5
votes
1answer
53 views

Go forward in bash argument history

I know about Alt + . to show the previous argument in bash. But what if I went past the desired argument (pressing Alt + . one too many times) and I want to go forward in history? Is there a key ...
0
votes
0answers
41 views

Is there a bash IDE like there is for R? [duplicate]

Just wondering if there is a bash IDE like there is for R, meaning you type commands in a text area and send lines, blocks or selection to bash just like you would do with let's say Rstudio. My goal ...
2
votes
5answers
5k views

Bash rename 360 000 files using find -exec

I've got around 360 000 files like: ./001/1/1.jpg ./001/1/2.jpg ./001/2/1.jpg ./002/1/1.jpg ./003/1/1.jpg ... pattern: [60 dirs]/[1000 subdirs]/[4-10 files].jpg I want to rename files using rename ...
2
votes
1answer
31 views

Clarification for Bash documentation on disown builtin option -h

According to the documentation: To prevent the shell from sending the SIGHUP signal to a particular job, it should be removed from the jobs table with the disown builtin or marked to not ...
1
vote
0answers
31 views

Any caveats to using bash disown from a shell script as an alternative to screen?

I have a process which starts and outputs to a log file. The script which starts it launches it in a GNU screen and then follows the log with tail. The process will immediately begin writing to the ...
1
vote
2answers
62 views

How to print all subsets of a set? [duplicate]

How can I print all subsets of a set using bash script. like : {} , {1} ,‌{2} ,{1,2} for A={1,2} this is what I have already write but I always thinks there must be a better way also my script just ...
2
votes
2answers
373 views

How to get the name of the nearest interactive ancestor shell from a script?

I know that one can use the $- contains i or not or check if $PS1 is empty to tell if a shell is interactive. However, those solutions only work for the current shell. I have a bash script trying to ...
1
vote
1answer
34 views

How to use bash command completion so that only files within a particular directory are completed?

Suppose I have a bash function specialcat that cats a file in the ~/Special directory specialcat () { cat ~/Special/$1 } Suppose the ~/Special directory was set up like so: mkdir ~/Special ...
6
votes
2answers
5k views

Showing GNU screen session name and window title in shell prompt

Is there a way to present the GNU Screen session name and window title in the prompt of the shell (let us say, the Bash prompt defined by PS1)?
0
votes
0answers
29 views

command substitution within c style for loop in Zsh

I have a function printing a number myFunc() { echo 11 } Now I want to use the number in expression in c style for-loop: for (( i=1; i<=$(myFunc); i++ )); do echo "$i" done Zsh gave ...
8
votes
5answers
6k views

Intersection of two arrays in BASH

I have two arrays like this: A=(vol-175a3b54 vol-382c477b vol-8c027acf vol-93d6fed0 vol-71600106 vol-79f7970e vol-e3d6a894 vol-d9d6a8ae vol-8dbbc2fa vol-98c2bbef vol-ae7ed9e3 vol-5540e618 vol-...
1
vote
1answer
31 views

Readline bindings not working with Control and Shift modifiers

I'm tweaking with GNU Readline's key bindings in Bash. Specifically, I wish to achieve the following mapping effect: Control' ==> 'I' ControlShift' ==> "I" Control` ==> `I` Control[ ==>...
1
vote
1answer
43 views

bash for loop variable not changing

I am iterating over indexes which are used to create data file id's. When I create the file name (data_file) and echo it, it prints properly, but when I try to sed that variable into the desired files ...
0
votes
1answer
39 views

How to pass string with special characters to shell command in a script?

I'm writing a small script that will help me debug some permission problems. I am passing the parent folder I wish to examine and am able to specify any sub-folders which I want to ignore. I'm ...
0
votes
0answers
20 views

Sub process keyboard input layer

I have a bash script where I set up a stream playlist by user interaction. The user then press p to play the selection. This simply do something like: media-player --playlist media-player defaults ...
1
vote
1answer
50 views

Initilization script runs twice on SSH

Running Ubuntu 12.04. I have a script that sets up the environment, its run by /etc/bash.bashrc. (It may be set to run by other shell profiles inits, I didn't actually set it up myself) When I Ctr+...
4
votes
1answer
43 views

How do I search bash's history in vi mode for “foo.*bar”

I have my bash environment in vi mode (set -o vi). So I can type <ESC>/ followed by a word, followed by enter, and bash searches for the word in the history. Since / is also vi's method to ...
59
votes
6answers
101k 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
1answer
21 views

Conditional prompt for mercurial

I'm trying to have a prompt that looks like this: $ some-folder (patch-name): I was able to get that to work when I did the following: hg_ps1() { hg prompt " ({patch})" 2> /dev/null } ...
9
votes
5answers
9k views

Check if script is started by cron, rather than invoked manually

Is there any variable that cron set when it's running a program ? If the script is running by cron, I'll skip some parts; otherwise invoke those parts. How can I know if the bash script is started by ...
0
votes
1answer
27 views

Expect Script - bash script file not found [closed]

My expect script #!/usr/bin/expect -f #I tried replacing sh - with bash -s still no positive results spawn ssh xxxx@yyyy "sh -" < test.sh expect "password: " send "zzzzz\r" expect "$ " This ...
2
votes
2answers
98 views

Reading a file with IFS loop works only when no arrays are used

Using the following code, I'm trying to read a file into an array: GROUPS=() while IFS=: read -r g1 g2 g3 g4 do GROUPS+=("$g3") echo "$g3" done < /etc/group This doesn't work, it doesn'...
0
votes
1answer
44 views

How to grouped the following variable and command and read 1 by 1

I've following array and variable : httpurl="http://www.nnin.com" firstquery=$(curl -s -X POST -d "UID=user1&PWD=1111" $httpurl) name="firstcust" ip="105.105.0.1" httpurl="http://www.mmim.com" ...
12
votes
4answers
38k views

sudo over ssh: no tty present and no askpass program specified

I keep getting this error and have tried several ways discussed online to fix this and none are working for me. I have setup SSH keys so when I run 'ssh newton@host.com' it automatically logs me in, I ...
16
votes
5answers
2k views

How can I convert Persian numerals in UTF-8 to European numerals in ASCII?

In Persian numerals, ۰۱۲۳۴۵۶۷۸۹ is equivalent to 0123456789 in European digits. How can I convert Persian number (in UTF-8) to ASCII? For example, I want ۲۱ to become 21.
30
votes
5answers
35k views

Press space to continue

How do I stop a bash script until a user has pressed Space? I would like to have the question in my script Press space to continue or CTRL+C to exit and then the script should stop and wait ...
2
votes
2answers
56 views

Bash colors defined in .bashrc behaving oddly. I can't figure it out

When I type ls -l my command color defined in .bashrc is only used on the first item up until the directory or file name which has its own color defined. The proceeding lines seem to get a default ...
0
votes
3answers
47 views

Pad number within string and rename file [duplicate]

I have some filenames as such. a2.txt.gz d12.txt.gz k5.txt.gz m4.txt.gz ... one character followed by 1 or 2 numbers followed by .txt.gz. I would like to add single numbers with a zero before the ...
1
vote
0answers
36 views

What does `echo $_` output after `export foo=bar`?

This is from a comment at Understand the meaning of `$_` When I open a new tab in gnome terminal, I run immediately $ echo $_ EDITOR Is the output because the last line in ~/.bashrc is export ...
0
votes
2answers
95 views

How do I record all terminal input and output to a local file by default for each session?

I'm aware the script command can be used to record all keyboard input and screen output to a file, but this has to be invoked each time a terminal session is started. I keep timestamped versions of my ...
3
votes
3answers
2k views

Query list of servers for memory/cpu allocated

Running RHEL 6.2, trying to write a bash script to SSH to a list of remote servers, and write the CPUs and Total Memory to a file, one line per host in the following format: HOSTNAME1 CPUS: 12 ...
1
vote
3answers
10k views

I don't see .bashrc file in my /home and /etc/skel direcories

I am running linux mint 17.1 rebecca, but I have found it quite different with other distros I am used to, in /home and /etc/skel there's no .bashrc I don't know if i mess up with something or it is ...
23
votes
4answers
101k views

how can I add (subtract, etc.) two numbers with bash?

I can read the numbers and operation in with: echo "First number please" read num1 echo "Second number please" read num2 echo "Operation?" read op but then all my attempts to add the numbers fail: ...
2
votes
0answers
95 views

“-bash: /root/.bashrc: Permission denied” when logging in

I have this server running Centos 7. Every time I log in (remotely or locally) with every user I get an error. If I log in as root I get -bash: /root/.bashrc: Permission denied If I log in as a ...
0
votes
1answer
16 views

creating shortcuts in Mac OSX at command line

I run a MacBook Pro with yosemite OSX. I'm trying to create shortcuts to go to certain directories, run script files, etc. This is my first attempt at creating shortcuts/aliases. I'm asking in this ...
-3
votes
2answers
71 views

find all files excluding one directory

so I want to clean a directory before I start work. I looks a bit like this. ./static ├── css │   └── styles ├── img │   └── pictures └── js └── scripts I want to rm the contents of css and js ...
2
votes
1answer
403 views

Read from and append to file at the same time while preserving end-of-line

Conceptually I have an easy task... I have [loosely] structured data in a file: Testing: debug, default CXXFLAGS <100's of additional output lines> Testing: release, default CXXFLAGS <100's ...
1
vote
2answers
341 views

cpulimit on a bash script which runs other commands

I have a bash script which runs other cpu-intensive commands . When I apply cpulimit on the bash script, the output of top shows processes for the commands inside the script still run without ...
2
votes
3answers
770 views

Why does `bash -c 'echo $0 ' ` output “bash”?

$ bash -c 'echo $0 ' foo foo $ bash -c 'echo $0 ' bash From bash manual ($0) Expands to the name of the shell or shell script. This is set at shell initialization. If Bash is invoked ...
0
votes
1answer
61 views

Script wont return when executed via SSH

I am calling a script via ssh client like this: ssh root@host "su - oracle -c '~/bin/weblogic start'" The ~/bin/weblogic is a bash script that actually starts the web server in the background and ...
-2
votes
1answer
51 views

Why does `bash -c echo hello` not output anything, while `bash -c “echo hello”` output “hello”?

Why does bash -c echo hello not output anything, while bash -c "echo hello" output "hello"? Thanks.
2
votes
1answer
51 views

GNU Parallel: How can I reference array elements?

This is my case scenario: luis@Balanceador:~$ echo ${array[@]} a b luis@Balanceador:~$ echo ${array[1]} a luis@Balanceador:~$ echo ${array[2]} b luis@Balanceador:~$ parallel echo ${array[]} ::: 1 2 -...
0
votes
1answer
34 views

Unix command to return maximum float value out of two after subtraction

This is similar to question about getting the minimum/maximum of two numbers, but I'm interested to do the same with float numbers. So basically I'm trying to use shell arithmetic expansion using the ...
7
votes
1answer
110 views

Highlight current bash prompt line

I want the terminal to highlight the current bash prompt line. The effect I'm trying to acheive is the same as in text editors where the current line with the cursor is highlighted. Something like ...
44
votes
8answers
4k views

Why does Bash's source not need the execution bit?

with Bash's source it is possible to execute a script without execution bit set. This is documented and expected behavoir, but isn't this against the use of an execution bit? I know, that source ...
4
votes
5answers
1k views

List files with line count and date

I want to be able to list files showing the number of lines the each file has and the date. I can happily get the line count using wc -l *. Not a problem. I can get the date using ls -l. Is there a ...
13
votes
8answers
11k views

Is there a unix command that gives the minimum/maximum of two numbers?

I was looking for a command to limit numbers read in from stdin. I wrote a little script for that purpose (critique is welcome), but I was wondering if there was not a standard command for this, ...
63
votes
16answers
76k 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 ...
0
votes
1answer
31 views

What are the possible correct combination for std-out(2) and std-err(1)?

sqlite -echo test.sqlite < test44.sql > test44.out 1>&2. sqlite -echo test.sqlite < test44.sql > test44.out 2>&1. I used the first command to get an output of (stdout and ...