Sample Header Ad - 728x90

Unix & Linux Stack Exchange

Q&A for users of Linux, FreeBSD and other Unix-like operating systems

Latest Questions

3 votes
1 answers
200 views
Why SIGTSTP (^Z/Ctrl-Z/suspend) doesn't work when process blocked redirecting write into a FIFO/pipe not open for reading yet? And what to do then?
Sometimes, a command is stalled attempting to write to a FIFO/pipe that no other process is currently reading from, but typing Ctrl - Z to put the process to the background by sending it the SIGTSTP signal (i.e. the *suspend* non-printing control character `^Z`) does work. Example: $ mkfifo p $ ll p...
Sometimes, a command is stalled attempting to write to a FIFO/pipe that no other process is currently reading from, but typing Ctrl-Z to put the process to the background by sending it the SIGTSTP signal (i.e. the *suspend* non-printing control character ^Z) does work. Example: $ mkfifo p $ ll p prw-rw-r-- 1 me me 0 Jul 30 16:27 p| $ echo "Go!" >p # Here & has been forgotten after the >p redirection (stalled) [Ctrl-Z] ^Z # Pressing Ctrl-Z just prints "^Z" on the terminal, nothing else happens [Ctrl-D] # Attempting to send the EOF character (nothing) # Doesn't print "^D" and does nothing and this is the behavior even though SIGTSTP (or susp) is reported to be attached to ^Z: $ stty -a speed 38400 baud; rows 24; columns 91; line = 0; intr = ^C; quit = ^\; erase = ^?; kill = ^U; eof = ^D; eol = ; eol2 = ; swtch = ; start = ^Q; stop = ^S; susp = ^Z; rprnt = ^R; werase = ^W; lnext = ^V; discard = ^O; min = 1; time = 0; -parenb -parodd -cmspar cs8 -hupcl -cstopb cread -clocal -crtscts -ignbrk -brkint -ignpar -parmrk -inpck -istrip -inlcr -igncr icrnl -ixon -ixoff -iuclc -ixany -imaxbel iutf8 opost -olcuc -ocrnl onlcr -onocr -onlret -ofill -ofdel nl0 cr0 tab0 bs0 vt0 ff0 isig icanon iexten echo echoe echok -echonl -noflsh -xcase -tostop -echoprt echoctl echoke -flusho -extproc How comes that in this case the terminal does not catch and transmit to the process the SIGTSTP signal generated by the Ctrl-Z? Is it because redirecting with > to a FIFO/pipe puts the terminal in raw mode (cf. The TTY demystified )? But if so, why does "^Z" get printed on the screen, and why Ctrl-D (EOF) doesn't print anything and yet doesn't have any effect either? Incidentally, is there an alternative way to send the stalled process to the background in such cases (instead of just terminate the process with Ctrl-C (i.e. ^C / SIGINT)?
The Quark (402 rep)
Aug 5, 2025, 08:47 AM • Last activity: Aug 5, 2025, 12:02 PM
2 votes
1 answers
486 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 29 Jun 23:18 test -rw-r--r-- 1 root wheel 0 29 Jun 23:18 test* ``` I can't work out how to spe...
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 29 Jun 23:18 test
-rw-r--r--  1 root  wheel  0 29 Jun 23:18 test*
I can't work out how to specify the second file test* with $(). List both files:
host:/tmp/backtick-test# ls test*
test  test*
List only the file with the trailing *:
host:/tmp/backtick-test# ls test\*
test*
Now with commmand expansion:
host:/tmp/backtick-test# echo $(ls test*)
test test test*
Returns three filenames.
/tmp/backtick-test# echo $(ls -al "test*")
-rw-r--r-- 1 root wheel 0 29 Jun 23:18 test test*
Better, returns two but I only want the second one.
Scott (103 rep)
Jun 29, 2025, 01:42 PM • Last activity: Jun 30, 2025, 06:13 AM
0 votes
2 answers
65 views
One-liner piping from find/xargs with paths including spaces
The following question likely does not relate specifically to Vim. I use a Vim example, as this is where I encounter the issue. Working on Ubuntu, I often open multiple files in Vim using tab pages: ``` $ vim --help | grep tab -p[N] Open N tab pages (default: one for each file) ``` I also use `find`...
The following question likely does not relate specifically to Vim. I use a Vim example, as this is where I encounter the issue. Working on Ubuntu, I often open multiple files in Vim using tab pages:
$ vim --help | grep tab
   -p[N]		Open N tab pages (default: one for each file)
I also use find with xargs and grep -l to obtain a list of files.
find . -type f -name "*.txt" | xargs grep -l "zod"
I can then quickly review the files output by find in vim:
vim -p find . -type f -name "*.txt" | xargs grep -l "zod"
The earlier grep command would fail if there are spaces in the files or directories, so -print0 can be added to the arguments to find; and -0 can be added to the arguments to xargs. The following creates a MWE set of sample files and directories including spaces:
echo zod > xx.txt && echo zod > 'x x.txt' && mkdir aa && echo zod > aa/xx.txt && echo zod > 'aa/x x.txt' && mkdir 'a a' && echo zod > 'a a/xx.txt' && echo zod > 'a a/x x.txt'
The following will then list the 6 text files we expect:
find . -type f -name "*.txt" -print0 | xargs -0 grep -l "zod"
But if I then try to pass the output of this command to vim tab pages (as below), the paths including spaces are split, and opened as 2 existent, and 9 non-existent files. Is there a way to get past the problem?
vim -p find . -type f -name "*.txt" -print0 | xargs -0 grep -l "zod"
I am keen to avoid side-effects such intermediate files or shell/environment variables (such as used in the top answer to a similar question [here](https://unix.stackexchange.com/a/597765)) ; and so I am looking specifically for a single-line command.
user7543 (274 rep)
May 21, 2025, 03:11 PM • Last activity: May 22, 2025, 01:23 PM
1 votes
1 answers
117 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](https://unix.stackexchange.com/a/82183/128023). In a plain command pipeline, both explicit (`\`) and implicit line continuation work fine: ```shell $ echo 'blah foo bar' \ > | grep -F 'blah' blah foo...
I'm trying to break a long command substitution on to multiple lines, as discussed in [this answer](https://unix.stackexchange.com/a/82183/128023) . In a plain command pipeline, both explicit (\) and implicit line continuation work fine:
$ echo 'blah foo bar' \
> | grep -F 'blah'
blah foo bar

$ echo 'blah foo bar' |
> grep -F 'blah'
blah foo bar
Inside of a command substitution, they both fail, and slightly differently:
$ foo=$(echo 'blah foo bar' \
> | grep -F 'blah')
-bash: 4131
| grep -F 'blah') +1 : syntax error: invalid arithmetic operator (error token is "'blah') +1 ")

$ foo=$(echo 'blah foo bar' |
> grep -F 'blah')
-bash: 4133
grep -F 'blah') +1 : syntax error in expression (error token is "grep -F 'blah') +1 ")
I'm on bash 5.2.21 and .37. I'm testing at the command prompt, but my real use case is this in .bashrc:
SSH_AUTH_SOCK="$(\ls -l /tmp/ssh-*/agent.* 2> /dev/null | grep -F "$USER" | head -1 | awk '{print $NF}')"
What's happening? Where did the +1 come from? And is there a way to do this correctly? (I'm interested in portable solutions or bash-specific.) ### Edit: More context I'm using [Oh My Posh] and [Atuin] with [bash-preexec], all of which do something at prompt time. My example actually looks like this: Screenshot of example showing fancy prompt They're set up in .bashrc like so:
eval "$(oh-my-posh init bash --config "${omp_config}")"                                                                                                                                                          # Get bash history number:
set_poshcontext () { export _myhistcmd=$(( $(fc -l -1 | cut -f1) +1 )); }
#                                                 Note the +1! ^^

[ -r "${HOMEBREW_PREFIX}/etc/profile.d/bash-preexec.sh" ] && . "${HOMEBREW_PREFIX}/etc/profile.d/bash-preexec.sh"
eval "$(atuin init bash --disable-up-arrow)"
To @Stéphane Chazelas questions:
$ typeset -p PS{1..4}
declare -- PS1="\$(_omp_get_primary)"
declare -- PS2="\$(_omp_get_secondary)"
-bash: typeset: PS3: not found
declare -- PS4="+ "

$ echo "$PROMPT_COMMAND"
__bp_precmd_invoke_cmd
_omp_hook
__bp_interactive_mode

$ trap
trap -- '__bp_preexec_invoke_exec "$_"' DEBUG
Jacktose (533 rep)
Apr 30, 2025, 11:58 PM • Last activity: May 2, 2025, 05:58 PM
82 votes
7 answers
37804 views
How can I get bash to exit on backtick failure in a similar way to pipefail?
So I like to harden my bash scripts wherever I can (and when not able to delegate to a language like Python/Ruby) to ensure errors do not go uncaught. In that vein I have a strict.sh, which contains things like: set -e set -u set -o pipefail And source it in other scripts. However, while pipefail wo...
So I like to harden my bash scripts wherever I can (and when not able to delegate to a language like Python/Ruby) to ensure errors do not go uncaught. In that vein I have a strict.sh, which contains things like: set -e set -u set -o pipefail And source it in other scripts. However, while pipefail would pick up: false | echo it kept going | true It will not pick up: echo The output is 'false; echo something else' The output would be The output is '' False returns non-zero status, and no-stdout. In a pipe it would have failed, but here the error isn't caught. When this is actually a calculation stored in a variable for later, and the value is set to blank, this may then cause later problems. So - is there a way to get bash to treat a non-zero returncode inside a backtick as reason enough to exit?
Danny Staple (2181 rep)
Oct 21, 2011, 10:42 AM • Last activity: Apr 29, 2025, 03:43 PM
20 votes
3 answers
103480 views
Zsh: export: not valid in this context
When running [this script](https://gist.github.com/amelio-vazquez-reina/0befbd311e96f8787754), I run into an error on [this line][1] (relevant snippet below): ... _NEW_PATH=$("$_THIS_DIR/conda" ..activate "$@") if (( $? == 0 )); then export PATH=$_NEW_PATH # If the string contains / it's a path if [...
When running [this script](https://gist.github.com/amelio-vazquez-reina/0befbd311e96f8787754) , I run into an error on this line (relevant snippet below): ... _NEW_PATH=$("$_THIS_DIR/conda" ..activate "$@") if (( $? == 0 )); then export PATH=$_NEW_PATH # If the string contains / it's a path if [[ "$@" == */* ]]; then export CONDA_DEFAULT_ENV=$(get_abs_filename "$@") else export CONDA_DEFAULT_ENV="$@" fi # ==== The next line returns an error # ==== with the message: "export: not valid in this context /Users/avazquez/anaconda3" export CONDA_ENV_PATH=$(get_dirname $_THIS_DIR) if (( $("$_THIS_DIR/conda" ..changeps1) )); then CONDA_OLD_PS1="$PS1" PS1="($CONDA_DEFAULT_ENV)$PS1" fi else return $? fi ... Why is that? I found this ticket , but I don't have that syntax error. I found reports of the same problem in GitHub threads (e.g. here ) and mailing lists (e.g. here )
Amelio Vazquez-Reina (42851 rep)
Jun 10, 2015, 02:11 AM • Last activity: Apr 18, 2025, 07:14 PM
2 votes
1 answers
2788 views
Job Control: How to save output of background job in a variable
Using Bash in OSX. My script has these 2 lines: nfiles=$(rsync -auvh --stats --delete --progress --log-file="$SourceRoot/""CopyLog1.txt" "$SourceTx" "$Dest1Tx" | tee /dev/stderr | awk '/files transferred/{print $NF}') & nfiles2=$(rsync -auvh --stats --delete --progress --log-file="$SourceRoot/""Copy...
Using Bash in OSX. My script has these 2 lines: nfiles=$(rsync -auvh --stats --delete --progress --log-file="$SourceRoot/""CopyLog1.txt" "$SourceTx" "$Dest1Tx" | tee /dev/stderr | awk '/files transferred/{print $NF}') & nfiles2=$(rsync -auvh --stats --delete --progress --log-file="$SourceRoot/""CopyLog2.txt" "$SourceTx" "$Dest2Tx" | tee /dev/stderr | awk '/files transferred/{print $NF}') When I use the & after the first line (to run the two rsync commands in parallel), my later call to $nfiles returns nothing. Code: osascript -e 'display notification "'$nfiles' files transferred to MASTER," & return & "'$nfiles2' transferred to BACKUP," & return & "Log Files Created" with title "Copy Complete"' Can't figure out what's going on. I need the 2 rsyncs to run simultaneously.
user192259 (51 rep)
Sep 30, 2016, 12:48 AM • Last activity: Mar 1, 2025, 02:01 PM
0 votes
1 answers
58 views
command substitution and Dollar sign before variable
I have a file named `fileWithOneCommand.txt` with just one command as follows ps -aux|head -n 5 then I write a testing shell script named 'test5.sh' with content as follow: file=/home/somepath/fileWithOneCommand.txt $file; echo see; cat $file; echo see2; $(cat $file); echo see3; but I cannot underst...
I have a file named fileWithOneCommand.txt with just one command as follows ps -aux|head -n 5 then I write a testing shell script named 'test5.sh' with content as follow: file=/home/somepath/fileWithOneCommand.txt $file; echo see; cat $file; echo see2; $(cat $file); echo see3; but I cannot understand the result, the result is as follow: $ ./test5.sh USER PID %CPU %MEM VSZ RSS TTY STAT START TIME COMMAND root 1 0.0 0.1 168584 10308 ? Ss Feb19 0:49 /sbin/init splash root 2 0.0 0.0 0 0 ? S Feb19 0:00 [kthreadd] root 3 0.0 0.0 0 0 ? S Feb19 0:00 [pool_workqueue_release] root 4 0.0 0.0 0 0 ? I' or 'ps --help ' for additional help text. For more details see ps(1). see3 1) $file should show the content of variable file, so should be output ps -aux|head -n 5 but why the output is the **execution result** of ps -aux|head -n 5, not just show ps -aux|head -n 5? 2) cat $file; return ps -aux|head -n 5, but why
$(cat $file);
return error "error: user name does not exist"? As I google command substitution, it said "the output of a command replaces the command itself. Shell operates the expansion by executing a command and then replacing the command substitution with the standard output of the command." so for $(cat $file); inside the bracket, cat $file return ps -aux|head -n 5 so why $(cat $file); not return the execution result of ps -aux|head -n 5 but return an error "error: user name does not exist"?
user1169587 (133 rep)
Feb 20, 2025, 04:09 AM • Last activity: Feb 20, 2025, 09:31 AM
0 votes
1 answers
37 views
Command substitution used with tput which doesn't print anything
I am doing a code-study of `/lib/lsb/init-functions` on mint 20 just to familiarise myself with SystemV init system. There is `log_action_end_msg` function in it which has this code snippet, ``` TPUT=/usr/bin/tput if log_use_fancy_output; then RED=$( $TPUT setaf 1) #=> Step 1 NORMAL=$( $TPUT op) # /...
I am doing a code-study of /lib/lsb/init-functions on mint 20 just to familiarise myself with SystemV init system. There is log_action_end_msg function in it which has this code snippet,
TPUT=/usr/bin/tput
if log_use_fancy_output; then
            RED=$( $TPUT setaf 1)  #=> Step 1
            NORMAL=$( $TPUT op)    #
            /bin/echo -e "${RED}failed${end}${NORMAL}" || true #=> Step 2
else
            echo "failed${end}" || true
fi
when the variable RED is assigned the value of $( $TPUT setaf 1 ), i expect RED to be assigned an empty string, since tput run on command-line doesn't print any stuff. But, I see the variable is passed onto echo in Step 2 implying that it has something stored in it. How does the variable RED get set, when tput doesn't print anything? (Or) Let us leave aside all the other stuffs. Take a fresh terminal and just run RED=$( $TPUT setaf 1) on my terminal and i expect terminal font color to be set as red but it is not so. Only when i do echo $RED, it turns red. Why is that tput command not getting run during the substitution step, but runs when you invoke echo.
Saravana (193 rep)
Feb 3, 2025, 08:13 PM • Last activity: Feb 3, 2025, 10:19 PM
-4 votes
1 answers
98 views
Why is command substitution resulting in 'command not found error'?
So doing ```lang-sh $(cat /etc/passwd) ``` results in "No such File or Directory" and ``` "$(whereis cat)" ``` results in "command not found" Why is command substitution not working in the expected manner?
So doing
-sh
$(cat /etc/passwd)
results in "No such File or Directory" and
"$(whereis cat)"
results in "command not found" Why is command substitution not working in the expected manner?
jstar100x (27 rep)
Dec 6, 2024, 03:54 PM • Last activity: Dec 6, 2024, 04:09 PM
0 votes
1 answers
100 views
bash - check free available space against requered
in bash i will run a script that take a look in the folder `folder123/` to know how much space is requered for the files they are in there. but this requered value will i multiplie with `1,5` and than subtract from the space they are available is at `.` if there are enough available space than go on...
in bash i will run a script that take a look in the folder folder123/ to know how much space is requered for the files they are in there. but this requered value will i multiplie with 1,5 and than subtract from the space they are available is at . if there are enough available space than go on in the script, but if not exit with an error. for checking how much space is requered i try this du -b folder123/ | tail -n 1 | awk '{print $1}' for checking how much space is available, i find out this df --output=avail -B 1 "$PWD" |tail -n 1 but how to multiplie the requered with 1,5 and than to subtract from the available, in bash-script? :edit if i use something like avail=$(df --output=avail -B 1 . | tail -n 1) req=$(( $(du -sb tempdir/ | cut -f1) * 3 / 2)) sum=$(printf '%d\n' "$((avail - req))") but how than to go on to check if $sum are oke?
user447274 (539 rep)
Oct 10, 2024, 04:31 AM • Last activity: Oct 10, 2024, 07:56 AM
1 votes
1 answers
81 views
How can I iterate over the white space separated words returned by a command substition?
I have the following simple shell command: for x in $(echo one two three); do echo $x; done When executed, it simply prints one two three on *one* line, i. e. the result of the command substitution is evaluated as though I'd quote `one two three`, like so: for x in "one two three"; do echo $x; done...
I have the following simple shell command: for x in $(echo one two three); do echo $x; done When executed, it simply prints one two three on *one* line, i. e. the result of the command substitution is evaluated as though I'd quote one two three, like so: for x in "one two three"; do echo $x; done However, I'd rather want the returned text of the command substitution to be returned as individual words, much like for x in one two three; do echo $x; done so that it prints one two three Is this somehow possible?
René Nyffenegger (2321 rep)
Sep 13, 2024, 03:02 PM • Last activity: Sep 14, 2024, 06:45 AM
292 votes
6 answers
46455 views
What's the difference between $(stuff) and `stuff`?
There are two syntaxes for command substitution: with dollar-parentheses and with backticks. Running `top -p $(pidof init)` and ``top -p `pidof init` `` gives the same output. Are these two ways of doing the same thing, or are there differences?
There are two syntaxes for command substitution: with dollar-parentheses and with backticks. Running top -p $(pidof init) and `top -p pidof init ` gives the same output. Are these two ways of doing the same thing, or are there differences?
tshepang (67482 rep)
Jan 13, 2011, 11:02 AM • Last activity: Aug 20, 2024, 12:29 PM
0 votes
1 answers
54 views
Nested command substitution doesn't work, but hardcoded results work
I am trying to pass filenames retrieved from [everything voidtools][1] via their es.exe command line (it works similarly to mlocate) to an "image previewer" like gwenview or nsxiv (in [wsl][2] environemnt). This works: ```bash IFS=$'\n'; nsxiv $(es keyword1 keyword2) ``` But this doesn't work: ```ba...
I am trying to pass filenames retrieved from everything voidtools via their es.exe command line (it works similarly to mlocate) to an "image previewer" like gwenview or nsxiv (in wsl environemnt). This works:
IFS=$'\n'; nsxiv $(es keyword1 keyword2)
But this doesn't work:
IFS=$'\n'; nsxiv $(es $(wl-paste))
when keyword1 keyword2 is currently stored in system clipboard.
user@wsl:~$ wl-paste | od -c
0000000   k   e   y   w   o   r   d   1       k   e   y   w   o   r   d
0000020   2  \n
0000022
Why? I thought that $() command substitution can be nested. Instead nsxiv returns No such file or directory es is an alias that converts es.exe output to match wsl environment)
es() {
    # Invoke the es.exe with provided arguments and pipe its output
    # through sed to replace Windows-style line endings with Unix-style line endings,
    # then use xargs to handle newline-delimited input and pass each path as a single argument to wslpath.
    # -p argument - search paths
    # -sort date-modified - sensible default
    # 2>/dev/null - remove "xargs: wslpath: terminated by signal 13"
    /mnt/c/Users/user/Downloads/ES-1.1.0.27.x64/es.exe -p -sort date-modified -instance 1.5a "$@" | sed 's/\r$//' | xargs -n1 -d'\n' wslpath 2>/dev/null
}
Daniel Krajnik (371 rep)
Jul 28, 2024, 02:14 PM • Last activity: Jul 28, 2024, 02:42 PM
0 votes
0 answers
47 views
Command Substitution doesn't work with spaces
I'm trying to pass a list of image files to open gwenview. The only way that seems to work is to list them on command line. However, some file paths have spaces, so they have to be additionally quoted. I thought that this will work: `gwenview $(echo \"file1 with spaces.png\")` However gwenview only...
I'm trying to pass a list of image files to open gwenview. The only way that seems to work is to list them on command line. However, some file paths have spaces, so they have to be additionally quoted. I thought that this will work: gwenview $(echo \"file1 with spaces.png\") However gwenview only sees "file1" ignoring the rest of the text. I thought command substitution will faithfully replace $(echo "file1 with spaces.png") with "file1 with spaces.png" - what's going on?
Daniel Krajnik (371 rep)
Jul 26, 2024, 02:53 PM
6 votes
2 answers
18996 views
How do I make Bash not drop NUL bytes on input from command substitution?
I have this Linux shell command: echo $(python3 -c 'print("Test"+"\0"+"M"*18)') | nc -u [IP] [PORT] My intention is to pipe the output of the `print` statement to the netcat command. The netcat command creates a socket to some service that essentially returns an stdout of the string passed in. The p...
I have this Linux shell command: echo $(python3 -c 'print("Test"+"\0"+"M"*18)') | nc -u [IP] [PORT] My intention is to pipe the output of the print statement to the netcat command. The netcat command creates a socket to some service that essentially returns an stdout of the string passed in. The problem here is that when I try to run this command, I get this message: -bash: warning: command substitution: ignored null byte in input; and my null byte \0 gets ignored. But I don't want the null byte to be ignored. How do I tell the system to NOT ignored my null byte and take in the input exactly as I've specified. I have done some Google searches but honestly speaking they haven't helped much. Also, any link to some great article is much appreciated. -------------------------------------------------------------------------- **EDIT** Using printf worked. Ordinarily passing
-c 'print("Test"+"\0"+"M"*18)'
also worked. Valued @cas explanation. I guess I might be sticking to printf given it's faster (though speed isn't particularly a concern in my case). Thanks to all those who contributed :-).
Joker (199 rep)
Dec 24, 2021, 09:41 PM • Last activity: Jul 6, 2024, 05:35 PM
26 votes
2 answers
3575 views
The old ticks vs parentheses issue: confused
Since being corrected many years ago, I switched from backticks to $() for command expansion. But I still prefer the backticks. It is fewer keystrokes and does not involve the Shift key. I understand that the parentheses are preferable because it is less prone to the errors that backticks is prone t...
Since being corrected many years ago, I switched from backticks to $() for command expansion. But I still prefer the backticks. It is fewer keystrokes and does not involve the Shift key. I understand that the parentheses are preferable because it is less prone to the errors that backticks is prone to, but what is the reason for the rule to *never* use backticks?
Stephen Boston (2526 rep)
Oct 1, 2020, 08:48 AM • Last activity: Jul 4, 2024, 12:44 PM
0 votes
1 answers
3853 views
How to store clean Impala query results in a variable using command line options
I have an Impala query: var=`impala-shell --ssl -B --quiet -q " show tables in db_name"` and I want to store the output of this query in a variable. I am able to store it, but it is storing extra information that I want to remove by using Impala command line options. This is the extra information wh...
I have an Impala query: var=impala-shell --ssl -B --quiet -q " show tables in db_name" and I want to store the output of this query in a variable. I am able to store it, but it is storing extra information that I want to remove by using Impala command line options. This is the extra information which I am getting in the result and want to remove: (Starting Impala Shell without Kerberos authentication SSL is enabled. Impala server certificates will NOT be verified (set --ca_cert to change) Error connecting: TTransportException, TSocket read 0 bytes Kerberos ticket found in the credentials cache, retrying the connection with a secure transport.
Harkirat Singh (33 rep)
Mar 28, 2018, 04:18 PM • Last activity: Jun 25, 2024, 06:18 AM
0 votes
3 answers
98 views
Pass output of command line to awk variable
I am trying to normalise a data file using the number of lines in a previous version of the data file. After reading [these](https://unix.stackexchange.com/questions/475008/command-line-argument-in-awk) [questions](http://mywiki.wooledge.org/BashFAQ/082), I thought this could work: ```lang-shell awk...
I am trying to normalise a data file using the number of lines in a previous version of the data file. After reading [these](https://unix.stackexchange.com/questions/475008/command-line-argument-in-awk) [questions](http://mywiki.wooledge.org/BashFAQ/082) , I thought this could work:
-shell
awk -v num=$(wc -l my_first_file.bed) '{print $1, $2, $3, $4/num}' my_other_file.bed
but it throws this error:
awk: cmd. line:1: my_first_file.bed
awk: cmd. line:1:              ^ syntax error
Protecting the . with a backslash doesn't change anything, nor does using backticks instead of $(). How can I use the output of wc -l as an awk variable? This will all be happening inside a snakemake pipeline, so I am somewhat limited in terms of flexibility. Contents of my_other_file.bed:
chrUn_KI270548v1	0	50	0.00000
chrUn_KI270548v1	50	192	1.00000
chrUn_KI270548v1	192	497	0.00000
chrUn_KI270548v1	497	639	1.00000
chrUn_KI270548v1	639	723	0.00000
chrUn_KI270548v1	723	860	1.00000
chrUn_KI270548v1	860	865	2.00000
chrUn_KI270548v1	865	879	1.00000
chrUn_KI270548v1	879	991	2.00000
chrUn_KI270548v1	991	1002	3.00000
chrUn_KI270548v1	1002	1021	2.00000
chrUn_KI270548v1	1021	1093	1.00000
chrUn_KI270548v1	1093	1133	2.00000
chrUn_KI270548v1	1133	1222	1.00000
chrUn_KI270548v1	1222	1235	2.00000
chrUn_KI270548v1	1235	1364	1.00000
chrUn_KI270590v1	0	16	4.00000
chrUn_KI270590v1	16	46	5.00000
chrUn_KI270590v1	46	48	6.00000
chrUn_KI270590v1	48	95	7.00000
chrUn_KI270590v1	95	117	8.00000
chrUn_KI270590v1	117	130	9.00000
chrUn_KI270590v1	130	136	8.00000
chrUn_KI270590v1	136	138	7.00000
chrUn_KI270590v1	138	139	6.00000
Whitehot (245 rep)
Jun 20, 2024, 09:21 AM • Last activity: Jun 20, 2024, 01:19 PM
0 votes
2 answers
80 views
Bash scripting: When to use variable, when function?
basic, innocent question: In bash scripting, why ever using a function, if one can set a variable containing command substitution with the essence of the function - a certain command or set of commands, which is supposed to output a certain value? In other words: Does it matter, if one defines a var...
basic, innocent question: In bash scripting, why ever using a function, if one can set a variable containing command substitution with the essence of the function - a certain command or set of commands, which is supposed to output a certain value? In other words: Does it matter, if one defines a variable, or a function for a certain, desired output? When and why to implement it as a variable? When and why it's better to implement it as a function? Example: Let's say, there is a directory on your system, which contains a lot of sub-directories in 1st level, and you want to find out with a bash script, what's the most recently modified. In a bash script, you can define a variable
for it, and print out its content on demand:
#!/bin/bash

#    latest-directory-displayer
#                                                                                                                                                                                                                    
#    Copyleft 🄯 2024
#                                                                                                                                                                                                                    
#    This program is free software: you can redistribute it and/or modify                                                                                                                                            
#    it under the terms of the GNU Affero General Public License as published by                                                                                                                                     
#    the Free Software Foundation, either version 3 of the License, or                                                                                                                                               
#    (at your option) any later version.                                                                                                                                                                             
#                                                                                                                                                                                                                    
#    This program is distributed in the hope that it will be useful,                                                                                                                                                 
#    but WITHOUT ANY WARRANTY; without even the implied warranty of                                                                                                                                                  
#    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the                                                                                                                                                    
#    GNU Affero General Public License for more details.                                                                                                                                                             
#                                                                                                                                                                                                                    
#    You should have received a copy of the GNU Affero General Public License                                                                                                                                        
#    along with this program. If not, see .                                                                                                                                           

# Displays what's the most recently modified sub-directory within
# current directory.

# Tool variable set                                                                                                            
find="/usr/bin/find"
sort="/usr/bin/sort"
tail="/usr/bin/tail"
grep="/bin/grep"
sed="/bin/sed"

# Variable set                                                                                                                 
rece_dir="`"$find" . -maxdepth 1 -type d -printf '%T+ %p\n' | \
                   "$sort" | "$tail" -1 | "$grep" -o "/.*" | \
                   "$sed" 's/ /\\\&/g;s+$+/+'`"

# Function set                                                                                                                                                                                                     

################################## Main part ###################################                                                                                                                                     

printf "$rece_dir\n"
exit
Cute. But why not doing it like this?
#!/bin/bash

#    latest-directory-displayer
#                                                                                                                                                                                                                    
#    Copyleft 🄯 2024
#                                                                                                                                                                                                                    
#    This program is free software: you can redistribute it and/or modify                                                                                                                                            
#    it under the terms of the GNU Affero General Public License as published by                                                                                                                                     
#    the Free Software Foundation, either version 3 of the License, or                                                                                                                                               
#    (at your option) any later version.                                                                                                                                                                             
#                                                                                                                                                                                                                    
#    This program is distributed in the hope that it will be useful,                                                                                                                                                 
#    but WITHOUT ANY WARRANTY; without even the implied warranty of                                                                                                                                                  
#    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the                                                                                                                                                    
#    GNU Affero General Public License for more details.                                                                                                                                                             
#                                                                                                                                                                                                                    
#    You should have received a copy of the GNU Affero General Public License                                                                                                                                        
#    along with this program. If not, see .                                                                                                                                           

# Displays what's the most recently modified sub-directory within
# current directory.

# Tool variable set                                                                                                            
find="/usr/bin/find"
sort="/usr/bin/sort"
tail="/usr/bin/tail"
grep="/bin/grep"
sed="/bin/sed"

# Variable set                                                                                                                 

# Function set                                                                                                                 
display_latest_dir() {
    "$find" ./ -maxdepth 1 -type d -printf '%T+ %p\n' |
    "$sort" |
    "$tail" -1 |
    "$grep" -o "/.*" |
    "$sed" 's/ /\\&/g;s+$+/+'
}

################################## Main part ###################################                                               

display_latest_dir
exit
Same output, same basic inner workings, but one over variable, while the other over function. A minor difference I've spotted was different escape requirements. As soon as you put your set of commands into a variable as command substitution, it probably needs 1x more
\
wherever you've escaped with a back slash. Why not always use variables, instead of functions? Why not always use functions, instead of variables?
futurewave (213 rep)
Jun 7, 2024, 05:29 PM • Last activity: Jun 7, 2024, 07:07 PM
Showing page 1 of 20 total questions