Bash is the shell from the GNU project. It is the standard shell on Linux and often available on other unices. If your question applies to all Bourne/POSIX-style shells, use the /shell tag instead.

learn more… | top users | synonyms

3
votes
2answers
93 views

Execute bash commands over SSH while staying in interactive mode afterwards

Probably it is a weird thing I would like to achieve but: I would like to SSH to a remote host and then execute some bash commands like "alias" automatically to use them afterwards in an interactive ...
3
votes
1answer
42 views

Bash tab completion stop searching

When I am typing into bash and I press the "tab" key to auto complete, sometimes it takes a significant time. E.g., file IO to read directories takes >5 seconds, and thus I am hung waiting for IO to ...
5
votes
1answer
112 views

understanding the default value of IFS

In my GNU bash version 4.2.8, IFS has a default value of space, tab and line feed by default: usr@T42 ~ $ echo -n "$IFS" | hexdump -C 00000000 20 09 0a | ..| ...
2
votes
3answers
150 views

How to use rsync or scp to efficiently copy the files from machineB and machineC to machineA?

I am running my shell script on machineA which copies the files from machineB and machineC to machineA. If the file is not there in machineB, then it should be there in machineC for sure. So I will ...
3
votes
1answer
86 views

What does exec 3<&1 do?

I understand exec can do I/O Redirection on current shell level. but I only sees usage like exec 6<&0 # Link file descriptor #6 with stdin. # Saves stdin. exec 6>&1 # ...
8
votes
3answers
145 views

How to define similar bash function at once

I have these functions in ~/.bashrc: function guard() { if [ -e 'Gemfile' ]; then bundle exec guard "$@" else command guard "$@" fi } function rspec() { if [ -e 'Gemfile' ]; ...
1
vote
2answers
78 views

How to find out all the files in other machines using bash shell script?

I am trying to find out which files are in machineB and which files are in machineC. And I need to run my shell script on machineA to figure out which files are in machineB and which files are in ...
4
votes
3answers
101 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 ...
3
votes
2answers
141 views

Set difference from two files in unix

I am looking for an efficient way to achieve set difference from 2 files. I do know that grep -vxF -f setA setB gives me B - A. This is what I want, but my files are as follows: setA: 2 4 5 6 set B: ...
1
vote
2answers
41 views

create file from function

#!/bin/bash # trims trailing spaces and tabs from file, using awk utility function remove_trail() { [ ! $# -lt 2 ] || { echo "Usage: $FUNCNAME file-to-trim out-file"; return 1; } # args ...
3
votes
1answer
33 views

grepping with “shopt -s globstar extglob”

I have shopt -s globstar extglob in my ./bashrc. I'm searching for files that refer to string Foo in the following command: grep -Fwn Foo /**/src/**/*.@(h|cpp) Now I want to except all refers from ...
1
vote
4answers
52 views

Write program output to log file containing PID in its name

How can I start a program and write its output to a log file, where the log file contains the PID in its name? I tried program_a > log_$! which doesn't work since $! is the PID of the last ...
4
votes
2answers
61 views

Brace compression

I love brace expansion (e.g. rm file.{0..5}). I find it a lot easier to read than the expanded version. Is there a quick and easy way of doing the opposite? For example given the input ...
22
votes
5answers
2k views

Why is iterating over a file twice faster than reading it into memory and computing twice?

I'm comparing the following tail -n 1000000 stdout.log | grep '"success": true' | wc -l tail -n 1000000 stdout.log | grep '"success": false' | wc -l with the following log=$(tail -n 1000000 ...
3
votes
1answer
51 views

ext4, why 70k file takes 88 blocks

[root@xx]# cat -n create_extents.sh 1 #!/bin/bash 2 3 if [ $# -ne 2 ] 4 then 5 echo "$0 [filename] [size in kb]" 6 exit 1 7 fi 8 9 ...
2
votes
1answer
33 views

lockfile-progs utilities usage [duplicate]

There is another question that is related, but it is actually different, because I'm asking why the behavior of lockfile-create & lockfile-touch is different to the current contents of the manual ...
21
votes
7answers
813 views

Should scripts that require sudo fail if they don't have it, or use sudo and prompt?

I have a script which gives me fine-grained control over my backlight brightness and requires sudo to run. It's essentially this: backlight="/sys/class/backlight/acpi_video0/brightness" echo $1 | tee ...
36
votes
2answers
2k views

Why does rm *(1)* remove all files in a directory?

I expected that: $ rm *(1)* would remove all files containing (1) in the name. I was wrong. It removed all files in the directory. Why?
1
vote
4answers
97 views

How can I delete the first dot before the first number?

How to remove the first dot before the first number? For example I have the following file more file Version …………2.465.76.8.332 I want to get only the version number - 2.465768.332
0
votes
3answers
101 views

Resolving MAC Address from IP Address in Linux

I need to write a bash script wherein I have to create a file which holds the details of IP Addresses of the hosts and their mapping with corresponding MAC Addresses. Is there any possible way with ...
2
votes
3answers
59 views

proper configuration of visudo NOPASSWD for bash backup script

The abstract question is: If script x calls program y, do I need a NOPASSWD entry in /etc/sudoers for x, y or both x & y? (And can x then call sudo -v without a password?) Details: I'm trying ...
2
votes
2answers
20 views

Symlink all subdirectories in a directory to another directory

I'm trying to symlink every directory in a folder to another folder. For example, in the structure below, I need the subfolders symlinked to folder2. - /home/chris/folder1 -- subfolder1 -- subfolder2 ...
0
votes
1answer
57 views

Bare-bones linux? [closed]

I am working on a homemade OS and I was originally going to write it myself in Assembly and Python, but I decided that doing this would just be easier, faster, and more Linux-y. I have the Python part ...
2
votes
1answer
42 views

How to use quick substitution to replace all the strings of the previous command? [duplicate]

I know I can replace a string of the previous command using ^old^new. But it seems to only replace the first occurrences as I do the follow: $ cat /proc/5742/task/5742/status ... $ ^5742^2839 cat ...
1
vote
1answer
58 views

Single command to login to SSH and run program?

Is there a way to structure a single command to login via SSH to a remote server and run a program on the remote login shell? In the OpenSSH manual, it reads "If command is specified, it is executed ...
1
vote
3answers
83 views

invoking bash with the -t option

Can anyone tell me about the -t option when invoking bash? I found the following handy command to use a lightweight terminal window as an application launcher urxvt -geometry 80x3 -name 'bashrun' ...
11
votes
3answers
454 views

Processing a Yes/No Response from Bash User

This very well may be stupidity on my part. I would like to accept Yes/No, True/False responses within my Bash script: test.sh #!/bin/bash ARGUMENT=$1 echo User passed in $ARGUMENT ...
2
votes
2answers
171 views

filter and count with grep in a single pass?

I am using grep at the end of a set of piped commands to filter some rows out of a file. Afterwards I will want to do some arithmetic based on the number of remaining rows. e.g. ...
4
votes
1answer
190 views

Most idiomatic way to set an optional parameter? [closed]

Suppose you have the following problem: Depending on if a script is executed within a pipe or not, you want to append the -print0 parameter to find. There are at least two ways to do this: [[ -p ...
2
votes
1answer
47 views

Completion of words on the screen as in VIM (Bash or Tmux)

How can I configure Bash or Tmux to complete the last matching identifier on the screen? Consider this common scenario: $ git fetch remote: Counting objects: 16, done. remote: Compressing objects: ...
5
votes
1answer
167 views

When is it important to write portable scripts?

Most code I write is in PHP. I have recently started learning shell scripting. Most of the resources and tutorials that I've come across are specific to Bash. Some warn about bashisms and some don't. ...
1
vote
1answer
71 views

How to exit out of the shell script successfully so that python subprocess think it is successfull exit? [closed]

Below is my shell script which simply execute a url like this http://example.com:8080/beat and parse the response and verify certain conditions on it. If that condition is met, then exit successfully ...
7
votes
3answers
314 views

Why are interactive shells on OSX login shells by default?

In Linux and, to my knowledge, all Unix systems, terminal emulators run interactive, non-login shells by default. This means that, for bash, the started shell will: When an interactive shell that ...
4
votes
1answer
54 views

One liner for padding columns (similar to printf) of output from the previous pipe command?

I'd normally use printf to pad each cell with enough whitespace to maintain header position over its respective column. I'm wondering now, whether there's a way to do this on one line. I'm currently ...
0
votes
4answers
172 views

Starting with bash: -lt and -gt arguments

I'm starting with bash and I found the following: if test $first -lt $second then echo $first is lower than $second else if test $first -gt $second then echo $first is higher than $second ...
3
votes
1answer
665 views

What's the difference between “==” and “=~”?

Given a string and a hash returned by openssl, why using == doesn't match them correctly but =~ does? Background question: How can I force a script to use more resources?
5
votes
2answers
354 views

Making sure a command has executed in bash

I have a bash file with commands to merge pcap files. I want to know if there is anyway to make sure a line in bash runs or not. Like in windows bat file we have %ErrorLevel% ¨ Say a command touch -r ...
0
votes
1answer
49 views

When executing a command, it is automatically suspended

The problem happened on one of my local machines with CentOS 6. I ssh to it from my MacBook trying to test a script, which is in /usr/local/bin. However, whenever I tried to execute the command, it is ...
-4
votes
3answers
131 views

How to get IP Address using shell script? [duplicate]

Want to get the IP address using Shell script. Without knowing the eth0 or eth1 or eth2 How to get the particular IP address. I am not interest to get localhost address, want to get private IP ...
-4
votes
4answers
86 views

How to add a 3 lines in a file using shell script? [closed]

I have a file, that file contain 10 lines. And now I want to add 3 lines to end of the file using shell script. After add lines, If I run the script again I don't want to add same lines again. ...
2
votes
4answers
115 views

Bash script to output path to USB flash memory stick

How would I return (output) the path to any USB flash memory stick(s) connected to the local computer using bash (Ubuntu and Linux Mint)? Background: I'm providing users with an automated backup ...
7
votes
7answers
505 views

Find out what command I last ran that starts a certain way?

On bash commandline environment, you can do !! to rerun your last command. And you can press up arrow to see the the last command you ran. But what if you want to only see but not run the last command ...
7
votes
1answer
324 views

Is this redirecting to /dev/null?

I found this script on the arch wiki which prefixes an ffmpeg command like this < /dev/null ffmpeg -i "$a" -qscale:a 0 "${a[@]/%flac/mp3}" I understand the ffmpeg command, it is the < ...
0
votes
3answers
76 views

Running a bash script each time a file is uploaded? [duplicate]

I have an FTP server (Debian) setup where users send JPG images. I need a process running on the background that every time a picture is sent via FTP a bash script is executed for generating ...
0
votes
2answers
79 views

How to write an alias function to make dd show progress bar?

According to this article, by replacing: $ dd if=/dev/sda of=/dev/sdb [additional options] with: $ pv -tpreb /dev/sda | dd of=/dev/sdb [additional options] one can augment the default dd ...
2
votes
3answers
50 views

How to list backup files missing corresponding real files?

I accidentally deleted some files spread across my home directory, but I do not know exactly which ones were removed. How can I get a list of all backup files missing their corresponding file? ...
6
votes
1answer
95 views

bash scripting - read tarball from stdin

I have a task I need to script that I feel should be stupidly simple, but I'm actually having a rather tough time. I have a short bash script that takes a specific type of application in tarball form ...
2
votes
0answers
34 views

“Debugging” with bashdb: why do I lose command echo after stepping into a simple script?

I heard about the bash debugger ( bashdb ) after reading a comment so I thought I should try it out and see if it can help me analyze bash scripts or more generally the execution of commands. There ...
2
votes
2answers
43 views

What does `env COLUMNS=%S ps …` do?

I have seen the syntax env COLUMNS=%sin shell scripts, e.g. see the line below from the Helm package in Emacs: env COLUMNS=%s ps -axo pid,user,pri,nice,ucomm,tty,start,vsz,%%cpu,%%mem,etime,command ...
0
votes
0answers
27 views

What do the number in the file metadata mean [duplicate]

I typed ls -l in the terminal to get the file metadata of all files in the current folder What do the circled number : 1 & 2 represent in regard to the file metadata ???