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
53 views

How do i run a command only after previous command is unsuccessful in bash? [duplicate]

We use && operator to run a command after previous one finishes. update-system-configuration && restart-service but how do I run a command only if previous command is unsuccessful ? ...
0
votes
1answer
60 views

predetermine script input text

I have a script that automatically sets up an emailing server based off what I enter for the following echo "Please enter your main domain: (Ex.: website.com)" read DOMINIO if [ $DOMINIO = "" ]; then ...
1
vote
2answers
59 views

Pipe ssh's output (stderr and stdout) to different commands

I'm a writing script for some kind of backup involving ssh and rsync. Every time that script runs I redirect all output to a log file. To standardize the log-entries I wrote a log-function that adds a ...
2
votes
1answer
34 views

How to set pipe of process after process substitution in bash so that process continues after script ends?

How can I set up a process that has had its output redirected via process substitution so that it can survive after the parent script exits? I am trying to set up a Bash script to monitor the output ...
1
vote
1answer
36 views

Why does this bash fork bomb not work on my RHEL VM?

I'm running a RHEL 6.5 VM in a VMware ESXi environment and accessing it through the vSphere Web Client virtual console (in my browser). I wanted to crash this machine on purpose and executed :(){ ...
0
votes
1answer
52 views

How to catch first line of stdout from bash process substitution?

I am working with the following block of bash and it is not printing out the first line of expected output: exec 3< <(python3 -m http.server) while true; do read -t 2 line ...
2
votes
7answers
170 views

Restart firewall from bash file

I need to restart firewall periodically, so I created a bash file to add it to CRON tables. In terminal, I restart firewall using command: service firewall restart but if I have similar line in ...
1
vote
3answers
34 views

Execute a database query by iterating through results of a previous query

I'm new to Unix and bash scripts so please bare with me if this is a silly question. I've just started working for a company that uses DB2 hosted on a Unix platform and there are a lot of unwanted ...
28
votes
6answers
3k views

Is 'cat' a shell built-in or an external program?

When I use the type command to find out if cat is a shell built-in or an external program I get the output below: -$ type cat cat is hashed (/bin/cat) -$ Does this mean that cat is an external ...
1
vote
4answers
58 views

Remove all brackets in filename

I've been trying to remove all brackets in my filename(s). Can anyone help me create a bash script to do that? This bash script below work flawlessly for non-bracket filename(s), but if there are any ...
2
votes
1answer
26 views

repeat bash history expansion substitution for all the matches

:& repeats bash history expansion substitution. For example: $ echo 1 1 1 1 1 1 1 1 1 1 $ ^1^9 echo 9 1 1 1 1 9 1 1 1 1 $ echo 1 1 1 1 1 1 1 1 1 1 $ ^1^9^:& echo 9 9 1 1 1 9 9 1 1 1 $ echo 1 ...
0
votes
3answers
58 views

backup script to copy files by its extension

I have created script to copy .png files from one directory to another. Script is as follows: i="`cat /usr/local/app1/default.conf | grep -i values | sed -e 's/\values=//g' -e 's/,/ /g'`" for data ...
1
vote
2answers
44 views

Find file which name contains given substrings simultaneously

In general, it's often needed to find a file whose name should contain several substrings simultaneously. How do I do that with bash? For example, "boost", "qt" and "linux" in a file called "How to ...
2
votes
3answers
193 views

Using sed command to find the first occurrence of the pattern and append

Say there is a text file as below: Hello world types="" Mario types="Game" All I want to do is find the first occurrence of type="" and append with word program My desired output: Hello world ...
2
votes
1answer
29 views

How do I make a custom “command not found” prompt for the terminal?

I want to make a custom "command not found" prompt for the terminal like this one. I have the template set up: ___________________________________________ / I see you're trying to use the ...
2
votes
3answers
44 views

Pasting some text with tabs into a here-document in a PuTTY window

I have a string with tab delimited data that looks like: h1 h2 a1 b1 a2 b2 I produced it with Notepad on Windows. I created tab delimited data, ensuring that tabs and not spaces are used. I ...
3
votes
2answers
52 views

Simultaneously check for empty output and successful exit status

I'd like to write the following test in an installer script1: if [ -n "`/etc/grub.d/30_os-prober`" ]; then install_dual_boot else install_linux_only fi However, it's also possible that ...
0
votes
1answer
41 views

How to create a bash script that sees all files in a directory and tells me their size and date of creation?

I'm a beginner in the field of programing/coding. I'm trying to write a simple bash script that will see all files in a directory, and then tell me each file's size and each of their dates of ...
1
vote
1answer
44 views

Nested quotes in subshells

Say I have to use quotes to encapsulate subshell output like: DATA="$(cat file.hex | xxd -r)" But I need to nest this kind of stuff like: DATA="$(cat file.hex | xxd -r | tr -d \"$(cat ...
2
votes
2answers
45 views

Trying to transplant bash to /mnt for chroot, what does bash want in order to work?

I have a stage 3 gentoo tarball.. extracted it to /mnt which is hosting /dev/sda3. Though there was no bin and no sign of bash after unpacking. Far as I'm aware, the rest of the configuration involves ...
0
votes
1answer
55 views

How can I set a bash_logout for all users?

I've run into an issue trying to set up a little bit of ASCII art to display upon connection and on logout. I can do this for one user with .bashrc and with .bash_logout, and set a login MOTD by ...
5
votes
5answers
247 views

How to use output redirection dynamically?

I'm trying to add a debug option to a script of mine. Normally I want to hide any output, like warnings etc, so I put >/dev/null 2>&1 behind a lot of commands. Now, when I want to debug my ...
1
vote
1answer
27 views

When was the upper and lower casing string substitution introduced into Bash?

Bash offers the ${PATH,,} and ${PATH^^} in its more recent incarnations to change a the case of a variable value ad hoc. When were those introduced? The Bash Cookbook from 2007 does not make any ...
2
votes
1answer
51 views

Is it possible to track bash commands in real time?

I'd like to know if I can track commands entered by user in a bash shell, in real time. What I'm trying to do is something similar to thefuck, but I need to prompt the user as and when he enters new ...
-2
votes
3answers
51 views

Removing column in field seperated file using scripting

The files is seperated by | pipe symbol myfile.txt Hello|how|are|you|hope|you|are|doing|fine Lilly|jasmine|rose|sunflower|nightfire|flowers I want to remove columns greater than 3. I want result ...
3
votes
2answers
172 views

Substitute for loop variable in a string pattern

I am trying to iterate over an array of file names and substitute the file name inside an absolute path. The code is, #!/bin/bash jsArray=(moment.js datatable.js jquery.js jquery.tmpl.js dt_jq_ui.js ...
1
vote
0answers
32 views

Is there any reason to prefer [[ over [ in bash? [duplicate]

I used to think [ would spawn /usr/bin/test and [[ was a builtin, but apparently, it doesn't: $ strace bash -c " [ -z "$FOO" ] && echo yes " 2>&1 | grep test and $ type [ [ is a ...
15
votes
3answers
780 views

Issue an error when using empty shell variables

Sometimes I use, $PROJECT_HOME/* to delete all files in the project. When the environment variable, PROJECT_HOME is not set (because I did su and the new user doesn't have this environment variable ...
2
votes
1answer
47 views

Piping a file that's being written and discarding the contents?

A software writes its logs on a file ("tonsOf.log"). Those logs are mostly useless and I process them later on with some pattern matching (line by line). So, for say 1 GB of logs, I end up with 40KB ...
2
votes
1answer
24 views

Why does this array related code print the index at the end of each line?

I'm having problems working with an array in BASH. I've simplified the problem down to the following code: #! /bin/bash A1[0]="user1 user2 user3" A1[1]="user4 user5 user6" for each in ${!A1[*]} ...
2
votes
1answer
41 views

Capture specific column from command output

I know how to redirect first 5 lines of the output to a file when I execute a command. I want to know how to redirect only specific information from the output. For example I would like to execute ...
10
votes
2answers
117 views

Correct behavior of EXIT and ERR traps when using `set -eu`

I'm observing some weird behavior when using set -e (errexit), set -u (nounset) along with ERR and EXIT traps. They seem related, so putting them into one question seems reasonably. 1) set -u does ...
1
vote
1answer
27 views

Set xterm title doesn't work over ssh

I followed unix s.e post and added the line, trap 'echo -ne "\033]0;$BASH_COMMAND\007"' DEBUG to my .bashrc file to set my last command as the terminal title. It works good on my ubuntu desktop ...
1
vote
3answers
93 views

String comparison in single brackets in zsh [duplicate]

Bash code to print all folders: for f in ~/*; do if [ $f == '/home/sk/.' -o $f == '/home/sk/..' ]; then true else echo "$f" fi done It works on bash. When i ran the code on z ...
1
vote
1answer
84 views

Why is Bash returning “No such file..” when pattern matching this string?

I have the following for loop: for f in ~/.lo-rcs/common/* do . $f done which is returning this error: bash: /home/sk/.lo-rcs/common/*: No such file or directory I keep classifying my bashrc ...
0
votes
0answers
30 views

Add slash suffix and not space for folder commands on bash auto-completion

What i expect my bash shell autocompletion to be like, % cd $HOM<TAB> -> cd $HOME/<CURSOR-HERE> What it does to me, % cd $HOM<TAB> -> cd ...
3
votes
4answers
136 views

Touching issue: Creating a filename from 2 variables

I am trying to create a filename from 2 variables. This is the error msg: touch: cannot touch `/root/tinstalls/2--06/06/15': No such file or directory 2--06/06/15 19:54 This is the code: ...
2
votes
0answers
47 views

Debian 8: Shell “service” command works quietly [closed]

I reinstalled Debian 8 Jessie on my vserver recently. I noticed an anomaly to previous versions: The service command shows no confirmation message anymore. If I want to restart e.g. the ssh service ...
0
votes
0answers
11 views

init.d/functions — get the program ids [duplicate]

This is the code block for function __pids_var_run, taken from CentOs - /etc/rc.d/init.d/functions # __proc_pids {program} [pidfile] # Set $pid to pids from /var/run* for {program}. $pid should be ...
5
votes
3answers
183 views

Assigning exit code to a shell local variable

#!/bin/bash function0() { local t1=$(exit 1) echo $t1 } function0 echo prints empty value. I expected: 1 Why doesn't t1 variable get assigned the exit command's return value - 1?
1
vote
0answers
41 views

set -x doesn't print stack info for bash autocompletion

In my bash 4.2.46 and centos - 7.1.1503, I ran set -x (to print debug info). Now, if I type ls, it prints: + ls --color=auto a b c ++ printf '\033]0;%s@%s:%s\007' root sk-vbox '~' All well and ...
0
votes
1answer
31 views

Custom bash prompt - cursor positioning issue [duplicate]

I know there are many related questions, however I couldn't find any other way to get help on this problem: On my CentOS VM I made a custom script for my bash prompt in /etc/profile.d/custom.sh ...
1
vote
1answer
52 views

Why does my bashscript not work?

So I made a bash script to automatically get ts3 installed and running on my vps, but for some reason it doesn't work. The error I get is: > /root/install/ts3install.sh: 4: cd: can't cd to ...
4
votes
5answers
87 views

Multiline Text Processing : Editing sshd_config

I have multiple blocks at the end of my sshd_config files like theses: Match User FOO ChrootDirectory /srv/www/FOO AllowTCPForwarding no X11Forwarding no ForceCommand internal-sftp ...
0
votes
3answers
46 views

What is `>&` in simple terms? [duplicate]

What does $ command 1>&2 do? As I understand it this runs the command but with the STDOUT redirected to the STDERR. But I'm not sure exactly how >& works. I have seen it as $ command ...
3
votes
1answer
133 views

How to log every command typed into bash and every file operation

I would like to log every command typed into bash (by either root or user) as well as every file operation. The idea is that I could write tools that could interrogate this log file and provide very ...
5
votes
1answer
53 views

How to stop a background process started in the same script, without exit the script?

I have a program that takes a variable time to run (no more than two minutes), and to make it less odious, I put fortune messages. #!/bin/bash ... for i in $(seq 1 45); do sleep 3; echo; echo; ...
0
votes
1answer
32 views

Can't run tmux although it's installed [duplicate]

I compiled tmux from source on Ubuntu 12.04 and it is installed in /usr/local/bin/tmux by default. However, when I type tmux in terminal I get the following message: $ tmux -bash: /usr/bin/tmux: No ...
0
votes
1answer
49 views

Why doesn't this simple piping with awk work? [duplicate]

I'm trying to follow this answer but I can't make it work. I simplified it down as much as I could to try work out what's going on (just running this at the terminal for now): rsync --progress -iav ...
2
votes
3answers
35 views

Local `set -e` for functions

Is there such a feature or can it be emulated reasonably easily? I want the same behavior except it should return where set -e would have caused a call to exit.