Questions tagged [bash]

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. For shell scripts with errors/syntax errors, please check them with the shellcheck program (or in the web shellcheck server at https://shellcheck.net) before posting here.

2,711 questions with no upvoted or accepted answers
Filter by
Sorted by
Tagged with
16 votes
1 answer
2k views

zsh can't input to terminal when piping stdin and stdout with variable command that has tty output

System info: macOS Sierra 10.12.6 zsh 5.4.2 (x86_64-apple-darwin16.7.0) GNU bash, version 4.4.12(1)-release (x86_64-apple-darwin16.3.0) Scroll to the EXAMPLES at the bottom if you just want to dig ...
user avatar
7 votes
0 answers
2k views

List all jobs in all shell sessions (not just the current shell), by current user

I know that the jobs command only shows jobs running in current shell session. Is there a bash code that will show jobs across shell sessions (for example, jobs from another terminal tab) for ...
user avatar
  • 262
6 votes
1 answer
2k views

Logging the output of remote commands on multiple ssh servers without delay

I've written a simple bash script that ssh's into 3 hosts (2 remote, 1 my own for testing) and runs a long running gui program that outputs text to the terminal that I'd like to log. #!/bin/bash ssh ...
user avatar
  • 161
6 votes
2 answers
2k views

Bash shell launched by forkpty() spawns child processes that ignore SIGINT. Why and how do I ensure SIGINT is not ignored?

I have a C program that uses forkpty to execute a bash shell. I'm finding that the programs launched by this shell are launched with SIGINT ignored, so when I send a Ctrl-C to the shell they never ...
user avatar
  • 161
6 votes
1 answer
1k views

bash/gnu readline multiline command: move row up/down

How to move one editing row up (back) or down (forward) in a multi-line command in bash/readline? Somethnig like gj/gk in Vim.
user avatar
5 votes
0 answers
298 views

Kernel module remove not permitted for some sudo users

I use a sudoers file, which (besides other items) contains following lines: homebridge ALL=(ALL) NOPASSWD: /sbin/modprobe -r bcm2835-v4l2, /sbin/modprobe bcm2835-v4l2, /sbin/reboot %sudo ...
user avatar
5 votes
1 answer
233 views

Bash autocomplete: List files and directories from other directory

I wrote a script called prpsls which creates or edits files inside the directory ~/proposals. Now I'm writing the autocomplete script for it, and I'm trying to set it up in a way that, after I hit Tab,...
user avatar
5 votes
0 answers
140 views

Bash completion own compgen prefix

I have a simple bash completion script that essentially invokes my (non-bash) program and set its output to COMREPLY, i.e. COMPREPLY=( $(my-program -- "${COMP_WORDS[@]}") ) Some of the options ...
user avatar
  • 161
5 votes
1 answer
506 views

How can I properly use an American flag emoji in my bash prompt?

I want to use an American flag emoji in my bash prompt (i.e. PS1 environment variable). However, the American flag emoji causes the terminal cursor to offset an extra character to the right. 🇺🇸 is ...
user avatar
5 votes
1 answer
260 views

Pasting into terminal including comments issue

This is somehow a follow-up to my last question: Pasting multiple commands into terminal stops at user input Pasting the following to the terminal works as expected: ( echo test1 # some comment echo ...
user avatar
  • 20.5k
5 votes
1 answer
1k views

Sending SIGINT to process groups sometimes gets ignored

I start a process group from bash. Then I send SIGINT to the entire process group. Sometimes the SIGINT kills the processes, sometimes does not. Why does SIGINT sometimes gets ignored ? I see ...
user avatar
5 votes
1 answer
572 views

Assign both menu-complete and dynamic-complete-history to TAB

Is there a way for me to assign both menu-complete and dynamic-complete-history to Tab (or one to Tab and the other to Tab-Tab) in .inputrc? I'm thinking there might be a way to setup a function and ...
user avatar
4 votes
0 answers
89 views

Ubuntu ls doesn't show all the files, but when I "ls filename", it shows up?

I am using PHP 7.3 to move some files around, in a larger app. The PHP 7 is running on Ubuntu 20.04.4 in a wsl2 container in Windows 10. On reading through other questions, it seems this is probably ...
user avatar
  • 41
4 votes
0 answers
67 views

Why does bash $LINENO change inside an inner block in a function?

I'm trying to debug auto completion script located in /usr/share/bash-completion/bash_completion (bash completion package on every distro) I did export PS4='+'$'\t''$LINENO'$'\t # \t for proper ...
user avatar
4 votes
0 answers
130 views

bash [-+]O shopt_name doesn't work the way it's supposed to as per by the manual

The bash manual says: " [-+]O [shopt_option] shopt_option is one of the shell options accepted by the shopt builtin (see SHELL BUILTIN COMMANDS below). If ...
user avatar
4 votes
1 answer
278 views

Why does ZSH finish executing a function after exit was called from a trap?

Consider the following script: trap "echo TRAPPEDTERM >&2; exit" TERM termself () { kill -TERM $$ echo "Survived TERM!" } termself echo "Bye." Executed ...
user avatar
  • 704
4 votes
0 answers
173 views

HISTCONTROL - When is erasedups executed?

I know that ignorespace and ignoredups work in real time and prevent entries going into the buffer, but when does erasedups come into play? Is it when the buffer is written/appended, is it when the ...
user avatar
  • 305
4 votes
0 answers
102 views

Is there a command like `time` that monitors the network traffic of commands?

I can use the time command to measure the time some command takes: time node -e "" node -e "" 0.06s user 0.01s system 92% cpu 0.076 total Is there a similar way to measure a ...
user avatar
  • 1,287
4 votes
0 answers
408 views

Merge stdout and stderr, and create copies of each separately

My goal is best explained by example. To start, here's a function that will produce sample output, on both stdout and stderr: output() { printf '%s\n' 1 2 printf '%s\n' errA errB 1>&2 ...
user avatar
  • 1,019
4 votes
0 answers
243 views

What are exactly shells levels?

My understanding of shell levels and sub-shell was that: Sub-shells are forks (but not execs) of the shell process created by ( ... ) constructs. $SHLVL counts shell execs. $BASH_SUBSHELL counts sub-...
user avatar
4 votes
0 answers
191 views

Colorize the PS4 prompt?

With the option set -x, each command is echoed to STDERR before execution, prefixed by the expanded $PS4, with the first character being repeated according to the call stack depth. I want the output ...
user avatar
  • 189
4 votes
1 answer
910 views

Adding "sudo" commands to the bash history of the *target* user

Something that's bothered me for many years is that if I want to search my shell history for commands which were run as root, I have to check two different locations: I have to check /root/....
user avatar
  • 146
4 votes
1 answer
376 views

Nested command substitution does not stop a script on a failure even if -e and shopt -s inherit_errexit are set

Suppose you have a following script sandbox.sh. (This looks similar to Command substitution inside a function does not stop the script on a failure even if -e is set, but I believe a bit different ...
user avatar
  • 157
4 votes
2 answers
1k views

Unable to have a local variable with the same name as a global read-only variable

#!/bin/bash readonly x=2 function test { local x=1 echo ${x} } test echo $x The result is, readonly-local-test.sh: line 6: local: x: readonly variable 2 2 When the variable is read-only this ...
user avatar
4 votes
0 answers
939 views

How to bind to ctrl+x in bash?

I appear to be using GNU bash, version 4.4.12, though it's possible my organization has modified it from HEAD. I'm trying to bind ctrl+x: $ bind -x '"\C-t": "echo testing"' <I press ctrl+t> ...
user avatar
4 votes
1 answer
1k views

Trap does not catch SIGTERM in child script

I have two scripts. First one is basically wrapper that calls subscript and sets timeout. #!/bin/bash # wrapper_script timeout --signal=SIGTERM 50 main_script.sh And if execution of main_script.sh ...
user avatar
  • 503
4 votes
1 answer
347 views

is it possible to colors the prompt for (reverse-i-search) context menu?

I am using fedora 25 but I think this is a general behavior on unix system. When I ctrl+r in a command-line environment I can start to type in and anything I type will get to be matched backward in ...
user avatar
  • 1,438
4 votes
0 answers
572 views

Coloring terminal commands ("syntax highligting" on command line)

I do know I can color my prompt using escape sequences in the PS1 variable. Now, I would like to know if there is a way to put some color on the bash commands I often use, on the command line itself. ...
user avatar
4 votes
0 answers
249 views

How to explicitly set the tabname of a new gnome-terminal?

I want to explicity rename a tab in gnome-terminal on startup of the tab. I don't want to use gnome-terminal --title flag as that gets reset by my systems bashrc file after whatever else is supposed ...
user avatar
  • 141
4 votes
2 answers
428 views

bash/cygwin broken after installing rtool?

Usually I can solve problems on my own (and Google). But recently I am baffled by this strange hiccup. Background: I use a chain of scripts for file-processing (in Windows, under Cygwin. Scripts ...
user avatar
  • 919
4 votes
0 answers
929 views

Bash command history stops working after starting new tmux session

My bash command history stops working after starting a new tmux session. No previous commands when typing up, down, reverse search, no output from running history. I've confirmed that this happens ...
user avatar
4 votes
0 answers
495 views

Replacing a running binary on read-only file system

I have a simple updater bash script, which replaces files by removing the incident old files and copying new files from an archive to the running embedded target root file system. This way, the script ...
user avatar
  • 171
4 votes
1 answer
2k views

TOR hidden service not always accessible through cURL. Takes multiple tries

When I try to access a hidden service on TOR using cURL, for some reason I'm not getting access to the site 100% of the time. Many times it returns "curl: (7) Can't complete SOCKS5 connection to 0.0.0....
user avatar
4 votes
2 answers
2k views

Bash root to user. Best in same or separate script?

I'm working on a Linux Bash script that needs to start as root. At the end of the script, it builds a list of packages. This requires running as user. Then it will move the packages, which needs to ...
user avatar
  • 109
4 votes
0 answers
340 views

Bash command not found due to stray garbage characters

What is the cause of this behavior? $ cd ../ $ bash: $'\210\210cd': command not found This happens sometimes (rarely) when I enter a command, there is no visible character preceding the cd, still it ...
user avatar
4 votes
2 answers
460 views

Context-sensitive bash completion changes wildcard behavior

Somewhere along the way I've fallen into the habit of hitting tab twice after using a wildcard in commands like mv or rm, which by default causes bash to show the list of files that would match the ...
user avatar
  • 386
4 votes
1 answer
2k views

Automating sshfs not working

I want to mount a remote directory and I want to automate this. So I write below script which not working properly. #!/usr/bin/expect -f spawn sshfs vedams@192.168.5.109:/home/user /mnt/remote expect ...
user avatar
4 votes
1 answer
161 views

git report moved lines

I want to track the number of additions and deletions that exclude moved lines. So if a commit has 10 additions, 5 deletions, and 3 moved lines, then I have 7 additions, 2 deletions excluding moved ...
user avatar
  • 181
4 votes
2 answers
2k views

Restarting a service from a script

I have a init script - /etc/init.d/myservice, which starts some processes, including a CLI process. And from this CLI process, I am adding a command which can be used to restart the service (restart ...
user avatar
  • 313
4 votes
1 answer
12k views

Create an iptables rule if the rule does not already exist

Hi I've created a script to setup Nagios on my remote servers. My script checks iptables to see if a firewall rule already exists and then is supposed to add the rule if it's not already there. ...
user avatar
  • 41
4 votes
1 answer
2k views

gnome "switch user" shortcut or command line

Is it possible to switch user of gnome desktop environment by single command or any shortcut key, in centos 6.0, or Can we create shortcut key? I found "fusa", but still unable to install it.
user avatar
  • 2,425
4 votes
0 answers
9k views

Using sshpass, return code (exit status) differs for reasons unknown using valid commands

There are a few things going on here but I think ultimately, either CLish or SSH is returning an exit code that is messing up my work flow. I'm attempting to connect to a remote machine (CLish shell) ...
user avatar
4 votes
1 answer
4k 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 ...
user avatar
  • 193
3 votes
0 answers
83 views

Why creating files in /dev/shm is not faster than creating files in /tmp?

Let's say I have two bash files. The first one is called diskFile.bash: for i in {1..10000} do file=$(mktemp) cat > $file <<- "SIGN" "Hello World" SIGN ...
user avatar
  • 319
3 votes
0 answers
65 views

Bash - Reading from file descriptor twice

I am working with Linux file descriptor at the moment to communicate with an external device via serial port, and I wonder if it is possible for me to duplicate the result returned the device. In ...
user avatar
3 votes
0 answers
55 views

Configuration or command to for case insensitive bash history search

Bash provides the interactive commands forward-search-history and reverse-search-history typically bound to the keys CTRL-s and CTRL-r respectively. Are there case-insentive equivalents or is there a ...
user avatar
  • 744
3 votes
0 answers
58 views

Same command sequence produces different results in command-line and script

I'm writing a utility script to help me delete Git branches in zsh. Currently, it looks like this: git for-each-ref --format="%(refname:short)" refs/heads/ | while read -r line; do ...
user avatar
3 votes
0 answers
32 views

Dynamically limiting the space dedicated to certain folders

I am currently building a server application on ubuntu 16.04, where users can register and upload their files in. To prevent people from uploading TB of data, i wanted to limit the size of the user ...
user avatar
3 votes
0 answers
71 views

2>&1 causes process to block

I am trying to debug what I have done wrong in GNU Parallel (>=20190122). This runs as expected: ( echo 1 | parallel-20190122 --pipe -j0 --tee dummy {} ::: {1..1000}; echo Parallel is done here:...
user avatar
  • 29.5k
3 votes
1 answer
347 views

How to not interpret string as command

I'm writing a script which should copy a client system and change some lines in a file. There is a source file which has a line like: $DB_HOST = "LegitDbHost"; And the destination file has ...
user avatar
  • 31

1
2 3 4 5
55