subshell

Shell detection of empty subshell

Shell detection of empty subshell SC1143 suggests that commented parts of a wrapped shell command to be wrapped in a subshell. Is the Posix shell "smart enough" to not launch a subshell when it sees that it does nothing? What about Bash and Zsh? Asked By: AvidSeeker || Source As ilkkachu writes, POSIX itself specifies …

Total answers: 1

Obtaining the delta for 'declare -F' between current and clean shell environments

Obtaining the delta for 'declare -F' between current and clean shell environments Main question: how would one get the delta for declare -F, between that in the current shell, and that as if the shell just started (first two commands below). $(declare -F) does not solve the problem because the subshell is a copy of …

Total answers: 1

Does the shell creating a subshell require the () Groups command?

Does the shell creating a subshell require the () Groups command? In my book (Sobell’s A Practical Guide to Linux, 4e) it is written that You can use the parentheses control operator to group commands. When you use this technique, the shell creates a copy of itself, called a subshell, for each group. It treats …

Total answers: 2

How to indicate that the shell was spawned by ncdu – NCurses Disk Usage?

How to indicate that the shell was spawned by ncdu – NCurses Disk Usage? I employ ncdu, the NCurses Disk Usage tool, in conjunction with zsh and p10k. Pressing ‘b’ triggers the spawning of a shell in the current directory. How can I present this information on-screen to ensure that I retain my previous mental …

Total answers: 1

Flock and bash strange chicken and egg problem

Flock and bash strange chicken and egg problem In which order are the distinct steps of this bash command done: (flock -n 9) 9> toto.txt If I do only the subshell part: (flock -n 9) I get this result: flock: 9: Mauvais descripteur de fichier (Wrong file descriptor). Hence, I would assume that the subshell …

Total answers: 1

Running a function as process with a set process name or id

Running a function as process with a set process name or id I have a bash script set up to monitor a number of UDP streams and convert it into actionable data. My problem is that I need to set the script to periodically check to see if the stream capture is running and restart …

Total answers: 1

Portable temporary file descriptors inside `sh -c`

Portable temporary file descriptors inside `sh -c` cat <(echo yes) Displays "yes". And running this inside sh -m results in the same thing on Bash 5.2.15. Yet on Bash 4.4.20 it throws an error: sh -c "cat <(echo yes)" sh: -c: line 0: syntax error near unexpected token `(‘ Why the error? Are there any …

Total answers: 1

Get exit status of the first command in a pipe in the second the command

Get exit status of the first command in a pipe in the second the command My question is similar to Get exit status of process that's piped to another, but I want to get the exit status of the second command in the pipe within the pipe. Here’s an example – false | echo $? …

Total answers: 2

Passing a variable to subshell

Passing a variable to subshell Contrived example: #!/usr/bin/bash MYVAR=$(cat /somedir | grep -i myval) Now I want: #!/usr/bin/bash BASEDIR=/somedir MYVAR=$(cat [BASEDIR?] | grep -i myval) How should variable be passed to subshell? Asked By: paulj || Source Variables are copied to a subshell. If you change their values, the change is not propagated back to …

Total answers: 2

How does a trap affect external programs?

How does a trap affect external programs? When a subshell is entered, traps that are not being ignored shall be set to the default actions Source: https://pubs.opengroup.org/onlinepubs/9699919799/utilities/V3_chap02.html#tag_18_28 Apparently, this is also true for external programs that I’m calling from my shell. trap "" SIGNAL works to trap (ignore) that signal. But trap action SIGNAL doesn’t …

Total answers: 1

Can a script be run as a subshell?

Can a script be run as a subshell? Scripts typically start with a shebang such as #!/usr/bin/env bash, which specifies the shell to be used for execution. The execution behavior when the shebang is not present seems to be up to the calling shell. Either way, the script is run from a "new" shell which …

Total answers: 2

Unable to get multicharacter field in fzf preview

Unable to get multicharacter field in fzf preview _select () { apt-cache search ” | fzf –prompt=’search: ‘ –marker="M" –ansi –layout=reverse –cycle –multi –inline-info –preview "(apt show $(echo {} | awk ‘ – ‘ ‘{print $1}’ 2>/dev/null) 2>/dev/null)" –preview-window="down:wrap:${PREVIEW_BORDER:-border-sharp}" -e } the following line gives me the first field _select | awk -F ‘ – …

Total answers: 1

Why is /dev/null needed to run asynchronous jobs in busybox sh?

Why is /dev/null needed to run asynchronous jobs in busybox sh? I’m curious why this special device is needed to fork the command and run it asynchronously in the minimal Busybox shell. BusyBox v1.30.1 (Debian 1:1.30.1-4) built-in shell (ash) Enter ‘help’ for a list of built-in commands. /bin/sh: can’t access tty; job control turned off …

Total answers: 1

Why do I need to disable=SC2031

Why do I need to disable=SC2031 Can you tell me, why I have to # shellcheck disable=SC2030 in the following script? get_names_and_hosts(){ unset LOCAL_HOSTS declare -a LOCAL_HOSTS unset LOCAL_NAMES declare -a LOCAL_NAMES multipass list –format json | jq -r ‘.list[] | [ .name, .ipv4[0] ] | @tsv’ | while IFS=$’t’ read -r name ipaddress; do …

Total answers: 1

How to print and use a command output in a one-liner?

How to print and use a command output in a one-liner? I’d like to pass the result of mktemp as argument to a command, let’s say gcc -o. gcc -o $(mktemp) causes the result to be used, but I need to figure out the result. The only thing I could come up with is gcc …

Total answers: 2

Why does a brace command group need spaces after the opening brace in POSIX Shell Grammar?

Why does a brace command group need spaces after the opening brace in POSIX Shell Grammar? TL;DR: Why does POSIX brace group need spaces after { reserved word but subshell doesn’t after reserved word (? POSIX shell grammar defines brace group and subshell as follows brace_group : Lbrace compound_list Rbrace subshell : ‘(‘ compound_list ‘)’ …

Total answers: 2

In Bash, how can I detect if I'm in a subshell?

In Bash, how can I detect if I'm in a subshell? I’m trying to write a function to replace the functionality of the exit builtin to prevent myself from exiting the terminal. I have attempted to use the SHLVL environment variable but it doesn’t seem to change within subshells: $ echo $SHLVL 1 $ ( …

Total answers: 3