Questions tagged [shell]
The shell is Unix's command-line interface. You can type commands in a shell interactively, or write scripts to automate tasks. Use this tag for questions applying to /bin/sh and most compatible shells (ash, bash, ksh, zsh, …). For shell scripts with errors, please check them in http://shellcheck.net before posting here.
12,290 questions
2
votes
1
answer
69
views
Wine cmd automatically escapes quotes, adds backslash
$ wine cmd /c 'echo "hello world"'
\"hello world\"
Where do the backslashes come from?
The command behaves differently in an interactive session:
$ wine cmd
Microsoft Windows 6.1....
3
votes
1
answer
128
views
Choice of field separator affects sort's ordering
Suppose we have a script named test_sort in our $PATH with the following contents:
#!/bin/bash
function echo_text () {
printf -- "%s\n" "$fc$oc$fs$lc"
printf -- "%s\n&...
0
votes
1
answer
30
views
Sorting output from find like tree's output
Here is a sample directory tree as it would appear if it were sorted in character code order (i.e., directories are not listed first):
${PREFIX}/
.bashrc
.include.sh.d/
common.sh
...
14
votes
2
answers
1k
views
Why do shell control statement have that syntax (semicolon/newline+then/do)
I of course realize the need to have something that separates the condition to the actual commands to be executed under the control statement, but why were it chosen to use both semicolon and a ...
0
votes
0
answers
34
views
LF file manager : how to go back to $OLDPWD?
What do I want :
On the file manager lf, we can bind some keyboard shortcuts to do either a lf command with :, or a shell command with $, !, or other symbols.
According to the documentation, there is ...
5
votes
2
answers
264
views
Why are there so many shell scripts using "x$var" = "xabc" instead of "$var" = "abc"? [duplicate]
In the old times of DOS batch scripts, the only way to check for a variable to be empty or to allow an empty variable was to prepend (or append) a known letter, commonly the letter x, because there ...
0
votes
0
answers
53
views
pgrep executable matching specific (current) user only
In a code review on Final look at my Lightshot print screen Linux handler POSIX shell script, specifically in this short answer, it has been pointed out to me, that if there are multiple X servers ...
0
votes
0
answers
43
views
User can't execute a shell script based on shebang but ok when interpreter directly invoked [duplicate]
Context
I have a system user caddy and a shell script placed under /run/caddy/caddy-cert-obtained-wrapper.sh.
/run and /run/caddy are owned by root:root with permissions 0755.
caddy-cert-obtained-...
-1
votes
2
answers
104
views
Unix folder has file named "-f". Standard `/bin/bash` tools do not recognized this (can not `rm`, `cat`, nor `stat` this). What to do? [duplicate]
Welcome to Termux
Docs: https://doc.termux.com
Community: https://community.termux.com
Working with packages:
- Search: pkg search <query>
- Install: pkg install <package>
- ...
2
votes
1
answer
617
views
What is the purpose of using && at the end of a sh script line?
I have seen code like this in a /bin/sh script:
repo_files=$(git ls-files) &&
There is no line continuation. What is the purpose of && at the end of a line like this? Can the '...
-4
votes
1
answer
227
views
How to redirect command output to a file if the command itself contains redirection
In the below shell script I'm executing the some command cmd remotely where in the command output is redirected to some file cmd_log. I'm getting the desired result as long as the command cmd itself ...
10
votes
4
answers
942
views
With `#!/bin/sh`, what is the closest to `;;&` (`bash`'s fallthrough)?
Without ;;&, /bin/sh gives Syntax error: ")" unexpected (expecting ";;").
;;& triggers shellcheck's ^-- SC2127 (error): To use cases with ;;&, specify #!/usr/bin/env ...
5
votes
1
answer
280
views
How to wait for subshells to finish from the outer shell?
If do this on my bash terminal:
(
(sleep 0.5 && echo 'first command, second result' &);
(echo 'second command, first result' &);
)
then I see:
second command, first result
...
7
votes
1
answer
278
views
Newlines in environment variable names under POSIX sh
This question asks how to get a list of environment variable names in POSIX sh. The top answer suggests invoking awk via the shell, but gives this caveat:
The output is ambiguous if the name of an ...
4
votes
3
answers
517
views
Shell (.exp) script only sending 1 of 2 files
I have a bash shell script that runs via cron. The script looks for 1 of 6 possible (data) files in a directory. If 1 or more of these files is found, a new (control) file is created for each data ...
3
votes
2
answers
144
views
Localising variables in /bin/sh functions
POSIX defines shell functions as:
fname ( ) compound-command [io-redirect ...]
The compound-command is further defined as either:
( compound-list )
{ compound-list ; }
In particular POSIX notes for ...
11
votes
5
answers
1k
views
Using `find` to find a file in PATH
I would like to be able to search all my $PATH for files matching a given pattern.
For example, if my PATH is /usr/local/bin:/usr/bin:/bin and there's a /usr/local/bin/gcc-4 and a /usr/bin/gcc-12, I ...
0
votes
1
answer
55
views
Storing the iterations of the Receiver (or node) number and RSSI value into a file
I'm quite new to Linux. Recently, I've been able to create a bash script that allows me to obtain the RSSI of the receivers (or node) with a running iteration. How can I store these results in an text ...
0
votes
1
answer
818
views
grep behaviour is different when run using bash -c '...'
I met an interesting issue while working with this code from Stack Overflow: tripleee's answer on "How to check if a file contains only zeros in a Linux shell?"
Why does the same bash code ...
1
vote
0
answers
659
views
Unable to change default shell to bash on new cachyOS installation [closed]
Updated
Just booted the PC and now it works.
>echo $SHELL
/bin/bash
>
Very strange. so loggin out and back in was not enough.
So should I close this question then?
Just installed new CachyOS ...
6
votes
1
answer
508
views
`rsync`: trailing slash vs wildcard
Suppose I want to sync the contents of a directory (source_dir) using rsync without creating a directory named source_dir in the target directory.
I can do this using rsync source_dir/ target_dir or ...
3
votes
2
answers
168
views
How do I autosource path and venvs in /bin/sh (posix sh) for docker pods
I have a minimalistic pod that holds a small python program.
I've added the source to $HOME/.profile but it does not autorun.
How can I autosource it when I log in?
docker run -ti <internal-docker-...
2
votes
1
answer
521
views
Using a literal asterisk (*) in a filename within command substitution in bash
how do you supply a literal asterisk * when specifying a filename to a command substitution in bash?
Consider the following directory:
host:/tmp/backtick-test# ls -l
total 0
-rw-r--r-- 1 root wheel ...
0
votes
0
answers
55
views
Is there any kernel filesystem extension that can recognize uri paths?
Is there any extension of linux filesystems that can solve uri paths in terminal to avoid doing things like:
cat https://example.com/file.txt
cat file:///some-file-path/file.txt
instead of
curl -vs ...
4
votes
0
answers
121
views
Recovering text of Unsaved document from process memory? (frozen Xed window - process still "running" but in Sleeping status)
The window of my text editor Xed froze with Unsaved documents just as I was doing 'File'->'Save as...' to save them... [How ironic.]
Since the process still exists, I am trying to recover the text ...
0
votes
0
answers
55
views
Remote Shell Python3 Stuck & stty raw -echo;fg Kali Linux Problem
So I've been doing CTF Tryhackme called mKingdom and got a remote shell. However, I want an upgrade of the remote terminal so I ran python3 -c 'import pty;pty.spawn("/bin/bash")' , it showed ...
0
votes
1
answer
92
views
Always terminate command on SIGHUP (nohup oposite)
There are processes that ignore SIGHUP signals like:
docker run -it -p 1521:1521 -eORACLE_PASSWORD=atk4_pass gvenzl/oracle-xe:18-slim-faststart
Currently, when I close the console, the docker command ...
0
votes
2
answers
120
views
how do I prevent script continuation when the process run queue is full?
Environment: shell is BusyBox bash 3.2 running in what started-out as Ubuntu Server many years ago, but has since been tweaked a great deal by the manufacturer of this particular box to become a ...
0
votes
1
answer
227
views
How to send SIGQUIT or SIGINT using bash shell to a shell script
According to BASH manual page,
When Bash receives a SIGINT, it breaks out of any executing loops. In
all cases, Bash ignores SIGQUIT.
So if there was a shell script, started from bash shell, to ...
2
votes
4
answers
175
views
Grep (BRE) on surrounding delimiters w/o consuming the delimiter? Counting delimiter-separated strings between filename and extension
I have a dataset of images labeled/classified by characteristics, where an image can have more than one label. I want to count how many of each identifier I have. A toy dataset is created below, with ...
0
votes
2
answers
99
views
How to temporarily substitute the login shell for running a shell command/subprocess?
My login shell is Fish, but I would like to execute a shell command (apt install ...) as if my login shell was Bash.
Is it possible to make a command/subprocess believe that my login shell is /usr/bin/...
11
votes
1
answer
1k
views
Why doesn't visudo, like sudoedit, spawn a text editor as a regular user?
As far as I know, sudoedit gives a user permission to edit a file that is owned and writable by root.
It makes a copy of that file, the copy is owned by the user, and then it spawns a text editor with ...
0
votes
0
answers
63
views
cgoups: how safe is changing cpuset of a process?
I've recently started to observe unexpected output (and later stty -a outputs terminal settings different from new shell tab) from my shell scripts project. As of now, I'm very close to be sure the ...
-4
votes
1
answer
89
views
Are simple command variable assignments accessible in nested scripts? e.g., does `child.sh` have "FOO=BAR" when running "FOO=BAR ./parent.sh"?
Although this is easy to try and see, I was looking for the answer on StackOverflow and couldn't find it. That's why I'm asking and answering my question.
I recommend reading this first. The answer is ...
1
vote
1
answer
100
views
Issue with `BASH_LINENO` in bash function which get debugging information
In Visual Code, on Windows 10, I'm trying to create a function for my .sh project (which I run with Git Bash), which will report errors to the console in case user, let's say, provides fewer or more ...
0
votes
0
answers
39
views
Why does bash man page state that quotes are removed when they are not actually removed after an unquoted expansion? [duplicate]
https://www.gnu.org/software/bash/manual/html_node/Quote-Removal.html
3.5.9 Quote Removal
After the preceding expansions, all unquoted occurrences of the
characters ‘\’, ‘'’, and ‘"’ that did ...
5
votes
1
answer
347
views
What is an embedded parameter expansion?
https://www.gnu.org/software/bash/manual/html_node/Shell-Parameter-Expansion.html
When braces are used, the matching ending brace is the first ‘}’ not
escaped by a backslash or within a quoted string,...
0
votes
1
answer
45
views
How to resolve a relative symlink to an absolute one without canonicalizing it?
On my machine, there is a file:
/var/lib/flatpak/exports/bin/com.github.tchx84.Flatseal
Which is a symlink whose content is:
../../app/com.github.tchx84.Flatseal/current/active/export/bin/com.github....
2
votes
1
answer
223
views
Unix or Linux command to compare binary files [duplicate]
I'm looking for a command that compares binary files.
Of course, I know about diff, but it is not very good at binaries.
I have two files from a error-prone source (scratched dvd) which should be ...
3
votes
1
answer
232
views
Replacing ./ with an alias
I'm trying to make an alias or a function that will execute a shell script or a file without holding up a terminal. So far I've figured out a command that'll do that, but I'm having trouble turning it ...
-3
votes
1
answer
139
views
Creating in Linux files in GB or MB range by various size by read a file [closed]
In Linux with Bash, there is the File numbers_in_one_line.
In this file there is only one line with several numbers, all separated by spaces.
These numbers are the value in bytes for creating files ...
9
votes
3
answers
874
views
How to delete buffered text written to terminal during script execution
Let's say hypothetically I'm executing a long-running bash script, and while it's running, I accidentally right click on the terminal and paste a sensitive password (freshly copied out of a password ...
4
votes
3
answers
614
views
Parse console output and audibly say error
I am testing testing app by running mycommand that outputs at the console and directly at /tmp/mycommand.log. Using zsh. mycommand is really a function with parameters, but I can get the same when ...
5
votes
2
answers
434
views
How (internally) does fd3>&fd1 after { fd1>&fd3 } put back (or not) original fd into fd1? ("bad file descriptor")
`I'm reading an answer to https://stackoverflow.com/questions/692000/how-do-i-write-standard-error-to-a-file-while-using-tee-with-a-pipe/692009#692009, https://stackoverflow.com/a/14737103/5499118:
{ {...
0
votes
1
answer
235
views
How to compare output of a program with a reference value in a shell script?
I have my own implementation of a Redis server which I'd like to test through a shell script.
The general idea is to feed it with some commands through nc, and since nc prints the output of my program ...
4
votes
1
answer
290
views
Run in background avoiding any job control message from the shell [duplicate]
Lets define a shell function (here the shell is Bash) and test it
$ s () { xterm -e sleep 5 & }
$ s
[1] 307926
$
[1]+ Done xterm -e sleep 5
$
With my specific meaning of ...
9
votes
2
answers
2k
views
What is this icon (dots over a gray curving line) that momentarily appears in my GNOME status bar?
The leftmost icon () is the one in question.
For context, I have a pair of multipoint Bluetooth earbuds simultaneously connected to both my iPhone and Linux laptop. When I'm listening to music on my ...
12
votes
1
answer
1k
views
Why doesn’t `sudo -E` preserve `PERL5LIB`?
I’m experiencing a strange behavior related to environment variable preservation with sudo on macOS(15.2): sudo -E doesn't preserve the environment variable PERL5LIB.
Reproduction Steps
Set an ...
0
votes
2
answers
136
views
How to redirect output from a program that waits for input
Following this post I created my own version of the script, with the difference that user and password are forwarded from the environment variables:
#!/bin/zsh
pamtester login $user authenticate <&...
3
votes
1
answer
176
views
Bash redirections - handling several filenames specially (man pages)
Please confirm/correct me. I've found related Duplication of file descriptors in redirection but that does not answer my specific question.
From the The GNU Bash Reference Manual, section 3.6 ...