All Questions
Tagged with bash-functions or bash
27,278 questions
0
votes
0
answers
82
views
Why are quotation marks and backslashes not working as expected in variables? [duplicate]
I'm trying to pass a command through to ffmpeg, but I'm struggling to get it to work with spaces in the name.
I've tried this:
filters="-vf subtitles='$filename.$format':force_style=Fontsize=24&...
1
vote
1
answer
126
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
40
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
357
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,...
10
votes
3
answers
2k
views
bash: echo "hello" | > file.txt results in an empty file
I use the Git Bash on Windows, and stumbled over a strange behaviour. When I execute this command, it creates an empty file:
echo hello | > hello.txt
(hello.txt is empty after this command)
I ...
4
votes
2
answers
734
views
Bash script: performance comparison of file reading methods
I'm trying to find the "right" way to read files line-by-line.
I have been using for for line in $(cat "$FILE"); do for a while, and I really enjoy its clearness.
I know that while ...
0
votes
1
answer
67
views
Bash, the -nt fails when comparing two rsynced files between APFS and HFS+
While writing a backup Bash script, I've copied files from an APFS file system to an external drive that is HFS+ (I don't want to convert it to APFS because sometimes I need to read/write files with ...
0
votes
1
answer
453
views
SLURM error and output files with custom variable name
I'd like my log files to be named after a variable. Since this isn't possible:
#SBATCH --output some_software.${var}.out
#SBATCH --error some_software.${var}.err
I came across this work around but ...
0
votes
1
answer
74
views
bash conditional variable assignment cli not what I expected [duplicate]
Can anyone explain what's going on with DR here?
user@host:~# DRYRUN=true
user@host:~# echo "$DRYRUN"
true
user@host:~# $DRYRUN && export DR="-n"
user@host:~# echo $DR
user@...
0
votes
0
answers
49
views
bash variable substitution inside heredoc delimiter: how does it work? [duplicate]
For a school project, I am tasked with making my own (simplified) shell, with bash being the reference point. This includes replicating heredoc behavior which was fun until I stumbled upon variable ...
0
votes
0
answers
105
views
Opening Vim under script(1) breaks output. Can Vim detect script?
Opening vim while recording with script causes script to go wild (my screen starts filling up with a bunch of crap, some of which says "script", and I also believe it is colored neon green).
...
3
votes
1
answer
337
views
Multiple selection with fzf and vim having new lines in path/file names
I have a Bash function that follows the fzf key-bindings in key-bindings.bash.
# shellcheck disable=SC2206
__fzf_edit_file__() {
builtin local -a opts
builtin local edit_file file
opts=(
&...
4
votes
1
answer
772
views
How can I remove special non-characters that are not part of national alphabets like faces from filenames, keeping any national alphabets intact?
How can I remove special non-characters e.g. faces, avatars, emojis, symbols that are not part of national alphabets from filenames while keeping any national alphabets intact?
e.g. ...
3
votes
1
answer
245
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 ...
1
vote
1
answer
179
views
How can I set a trap to print the name and exit status of each function as it's executed
I answered a recent question using the RETURN trap to store the name of the last function executed. I wanted to enhance it to also save the exit status and discovered that wasn't as easy and I couldn'...
2
votes
4
answers
227
views
Is there a Bash variable containing the name of the previously executed function?
I want to line up a couple of Bash functions based on the success or failure of the execution of the preceding function.
If something fails, I want to print an error message: function_x has failed.
...
4
votes
0
answers
113
views
Why is `fc` output different through a pipe in a subshell?
Why does echo "$(fc -l -1)" show the previous command, but echo "$(fc -l -1 | cat)" show the current command?
$ testfc () {
> echo "$(fc -l -1)"
> echo &...
0
votes
0
answers
33
views
Wildcard not expanding. sudo to blame? [duplicate]
RHEL 8.
$ alias suip
alias suip='sudo -iu postgres'
If I specify the complete filename, the ls command works as expected:
suip ls ~postgres/logs/pgbackrest_2025-04-30_01:00:01_incr.log
/var/lib/...
1
vote
1
answer
177
views
Multiline command substitution - syntax errors with mysterious `+1`
I'm trying to break a long command substitution on to multiple lines, as discussed in this answer.
In a plain command pipeline, both explicit (\) and implicit line continuation work fine:
$ echo 'blah ...
0
votes
0
answers
38
views
bash subshell execution behaves unexpectedly [duplicate]
I have a script which is supposed to fetch 2 URLs sequentially:
#!/bin/bash
wget_command='wget --restrict-file-names=unix https://www.example.com/{path1,path2}/'
$($wget_command)
echo $wget_command ...
0
votes
0
answers
33
views
Why is set -e also ignored for functions called via command substitution [duplicate]
I've seen questions about this topic, but they talk about constructs like if or ||.
I don't understand why the same behavior seem to happen for substitution ($())?
$ my_function() { echo "the ...
1
vote
0
answers
60
views
How to pretend to not run the command in a TTY [duplicate]
I have a command (systemctl status) that halts and waits for keystrokes if I run it in a terminal that is not tall enough. I'm not interested in this behavior and can't find an option to bypass it.
If ...
0
votes
3
answers
126
views
Making each word in a text file an item in a bash array
I have a string of text and spaces which looks like this:
macOS windows arch-linux ubuntu_linux
I want to append each item (with whitespace denoting a break between items) to a bash array. How do I ...
2
votes
1
answer
173
views
Using mapfile on the output of another command [duplicate]
I want to edit a file before feeding it into an array. The data file contains:
Line 1
Line 2
Line 3
If I read it with
mapfile -t direct < data
I obtain a 3-element direct array
But I pipe the ...
0
votes
1
answer
120
views
How to make bash not set the underscore _ environment variable?
How to make bash not set the underscrore _ variable for the processes that it's starting?
Explanation of what it is if you haven't encountered it before:
If you launch env from bash, you'll see the ...
0
votes
1
answer
115
views
Understanding `bash` options -Ee
In a larger script I have
trap -p
echo "settings: [$-], command: [$*]"
"$@"
echo "return code $?"
and I get the output
trap -- '_shunit_cleanup EXIT' EXIT
trap --...
6
votes
4
answers
988
views
Using curly braces to process colon-separated variables
If we do:
VAR=100:200:300:400
We can do:
echo ${VAR%%:*}
100
and
echo ${VAR##*:}
400
Are there any equivalents I could use to get the values of 200 and 300?
For instance, a way to get only what's ...
-3
votes
1
answer
168
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 ...
-2
votes
2
answers
138
views
Counting unique IP addresses per hour in existing log files
I am trying to determine the number of unique web visitors to my website each hour. The log files (access*.log) contain many lines looking similar to this.
192.168.211.85 - - [29/Sep/2024:10:17:02 -...
0
votes
1
answer
1k
views
Where to set XDG_RUNTIME_DIR per user?
So I ran into the following interesting problem:
I tried to define XDG_RUNTIME_DIR in my .bashrc as follows:
export XDG_RUNTIME_DIR=$MY_HOME/.local/share
After doing this I noticed my pactl cli ...
0
votes
1
answer
122
views
bash - list of breaking change
I know bash tries very hard to keep new versions compatible with previous ones.
However they sometime introduced (minor, but still) breaking changes:
for exemple when we could no longer have : { foo }...
3
votes
1
answer
84
views
Why can't I have a single quote in a bash associative array index when testing with `test -v` [duplicate]
When I run the following code
declare -A X # get an associative array
index="a'b" # a somewhat weird index
X[$index]="this is set"
echo "<<${X[$index]}>>"
if ...
14
votes
1
answer
3k
views
Why does cd '' succeed in bash?
Maybe I'm missing it, but I don't find it documented that cd '' should succeed. Since there is no directory with the name '', it seems obvious that it should fail. For example,
mydir=
cd -- "$...
2
votes
2
answers
173
views
How does `*\ *` work in bash?
There is an answer from SuperUser, which renames filenames containing whitespace:
for f in *\ *; do mv "$f" "${f// /_}"; done
The part I don't understand is *\ *.
The author wrote ...
2
votes
1
answer
274
views
Using Here-Document as password Input for ssh
First, I do know this is a security risk. I do know about ssh keys and sshpass. This belongs to a fun project.
I wondered if a bash Here-Document could be used as a password input for ssh.
I ...
-3
votes
1
answer
174
views
bash -n is a not documented in manpages or info documents [closed]
Even though bash -n is a valid and widely used option, some manpages don't clearly document it in the bash options section, even though it is implemented. It's also not in the GNU Bash Reference ...
5
votes
6
answers
2k
views
Why can't I merge folders by renaming one of them?
Try to run this
mv -- "foldername...__" "foldername..."
This will move folder foldername...__ under the folder foldername... instead of renaming it. Why?
Note that foldername... ...
0
votes
1
answer
115
views
How to create splittet random files and join them with dmsetup
In Linux in Bash, there is a script, a part of the script is this
while true ; do
echo
awk -v x=$(<"$TEMPDIR"size_container_in_byte) -v n=$(<"$TEMPDIR"parts) 'BEGIN{...
0
votes
1
answer
261
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
315
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 ...
2
votes
2
answers
779
views
Merging values from 2 YAML files in bash
A bash command:
$(System.DefaultWorkingDirectory)/yq_linux_amd64 '. *= load("${{ parameters.HELM_CHART_PATH }}/values/DEV/${{ parameters.COMPONENT }}.yaml")' ${{ parameters.HELM_CHART_PATH }}...
0
votes
2
answers
7k
views
How to check if env exist in bash?
What is the standard way, in bash, to check if an environment variable is set up? I'm a little confused between the following options:
! [ -z "${VAR}" ]
[ -n "${VAR}" ]
[ "${...
3
votes
1
answer
1k
views
how to pass environment variables to singularity exec
I have a BASH pipeline which at a point runs a Singularity container with singularity exec as follows:
singularity exec --bind `pwd`:/folder --bind $d:/results <image>.sif <tool_command> -...
0
votes
2
answers
149
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 <&...
1
vote
2
answers
115
views
bash: take exclusive control of the console/tty
I have a script which implements a simple menu using dialog (an ncurses app for generating TUI widgets). This runs immediately after boot on an Arch-based system.
The dialog script works when ...
3
votes
1
answer
185
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 ...
0
votes
3
answers
249
views
How to avoid a race condition where a script runs a program that should only be run once per script
I have a script that runs a program that I only want to run as a single instance per-script. For example, I have a script "a" and a script "b", which each runs a Java program. I ...
0
votes
1
answer
77
views
No space after option with bash completion
I'm trying to filter bash-completion suggestions. Here awf-gtk2 -s abc.png [TAB] display -h --help -s --screenshot (it's ridiculous) / and awf-gtk2 -s[TAB] add a space after -s (perfect), with:
...
0
votes
2
answers
166
views
How to expand a Bash alias given as an argument?
I probably approach this problem wrong. I update my Linux machines with bash aliases like:
alias upg-pi='\ssh -c [email protected] -C -i $ssh_identity -o ConnectTimeout=$ssh_timeout root@$...
2
votes
1
answer
218
views
Dump bash stack trace on error with function parameters
With the function
function fail() {
local msg="$*"
echo $msg at
for i in ${!FUNCNAME[@]}; do
echo " ${FUNCNAME[$i]} ${BASH_SOURCE[$i]}:${BASH_LINENO[$i]}"
done
exit ...