Skip to main content
Search type Search syntax
Tags [tag]
Exact "words here"
Author user:1234
user:me (yours)
Score score:3 (3+)
score:0 (none)
Answers answers:3 (3+)
answers:0 (none)
isaccepted:yes
hasaccepted:no
inquestion:1234
Views views:250
Code code:"if (foo != bar)"
Sections title:apples
body:"apples oranges"
URL url:"*.example.com"
Saves in:saves
Status closed:yes
duplicate:no
migrated:no
wiki:no
Types is:question
is:answer
Exclude -[tag]
-apples
For more details on advanced search visit our help page
Results tagged with
Search options not deleted user 22565

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.

9 votes

Why is set-o errexit breaking this read/heredoc expression?

In zsh, you can do: NUL=$'\0' IFS= read -d $NUL -r var << EOF 1 2 3$NUL EOF (zsh also understand read -d '' as a NUL delimiter like bash. read -d $'\0' also works in bash but that does pass an empty … That's however standard code, so you don't need to rely on the zsh/bash specific read -d. …
Stéphane Chazelas's user avatar
1 vote
Accepted

How to pass a file list to zcat as a bash variable?

lists are assigned with the var=('first value' 'second value'...) syntax in bash. paths=(/var/log/log.3.gz /var/log/log.2.gz /var/log/log.1.gz /var/log/log) zcat -f -- "${paths[@]}" | wc -l Beware that … zcat -f -- "${paths[@]}"; } | wc -l Or: zcat -f -- </dev/null "${paths[@]}" | wc -l If you wanted to concatenate the /var/log/log* files in reverse numeric order, you'd rather use the zsh shell than bash
Stéphane Chazelas's user avatar
3 votes
Accepted

Bash get the length of indirect variable expansion with a list as the variable

b} is ${(P)b} (and where ${#array[@]} can also be written $#array like in csh) $ a=(1 2 3 4) b=a $ echo ${(P)#b} 4 In bash, if you really wanted to use ${! … kind of trick to have a variable that expands dynamically to the number of elements in $a with: $ typeset -n b='x[(x=${#a[@]}),0]' $ echo "$b" 4 $ a+=(more) $ echo "$b" 5 (here using x instead of b as bash
Stéphane Chazelas's user avatar
1 vote
Accepted

How to pass multiple args to a single variable in bash?

In the yash or zsh shells, that can be simplified to "${@[1,2]}" (even to "$@[1,2]" in zsh), and in ksh93, bash and zsh to "${@:1:2}". pip39() {python3.9 -m pip "$1"} is zsh syntax however. … In bash or other Bourne-like shell, you'd need pip39() { python3.9 -m pip "$1";} (or simply pip39() python3.9 -m pip "$1" in the Bourne shell and most Bourne-like shells, though not bash, posh nor yash …
Stéphane Chazelas's user avatar
2 votes

Get total duration of video files in a directory in hours

That boils down to how to divide a number from the output of a command by 3600. Which you could do with: ... | awk '{print $0 / 3600}' Though here, you can do the whole thing with: exiftool -r -n -q …
Stéphane Chazelas's user avatar
2 votes

How can I count a python array that output from my last command?

As that looks like a json array, you could just pipe it to jq length
Stéphane Chazelas's user avatar
3 votes

set -A invalid in bash

is the ksh88 syntax, also recognised by pdksh and derivatives, ksh93 and zsh (in zsh, the -- after -A is not recognised nor needed except in ksh emulation), but not bash. array=(values ...) … ), or you could use the ksh ((...)) arithmetic evaluation operator also supported by bash and zsh: ((i ! …
Stéphane Chazelas's user avatar
8 votes
Accepted

IF test returning too many arguments for path comparison

Instead of doing lexical comparisons, also note that ksh/bash/zsh's [[....]] and most [ implementations including the [ builtin of bash support a -ef operator, to check whether two files are the same ( …
Stéphane Chazelas's user avatar
16 votes
Accepted

Bash -c with positional parameters

Otherwise, $0 would just be bash. Then you can do for instance: $ echo foo > foo $ bash -c 'wc -c < "${1?}"' getlength foo 4 $ rm -f bar $ bash -c 'wc -c < "${1?}"' … getlength bar getlength: bar: No such file or directory $ bash -c 'wc -c < "${1?}"' getlength getlength: 1: parameter not set Not all shells used to do that. The Bourne shell did. …
Stéphane Chazelas's user avatar
14 votes

All possible combinations of characters and numbers

In bash, you could try: printf "%s\n" {{a..z},{A..Z},{0..9}}{{a..z},{A..Z},{0..9}}{{a..z},{A..Z},{0..9}}{{a..z},{A..Z},{0..9}}{{a..z},{A..Z},{0..9}} but that would take forever and use-up all your memory … You can do that same loop in bash, but bash being the slowest shell in the west, that's going to take hours: export LC_ALL=C # seems to improve performance by about 10% shopt -s xpg_echo # 2% gain (against …
Stéphane Chazelas's user avatar
0 votes

how to pass variablees to subshell while command substitution in bash

Your code doesn't make sense. Maybe you meant: stockapp=$( PKG=$pkgname su -c 'pm path "$PKG"' | sed '/base/!d; s/package://g' ) That is, run the login shell of root as root with -c and pm pat …
Stéphane Chazelas's user avatar
2 votes

Extend multiple globbing patterns with same additional pattern at the same time

.<->.gz' ) expanded=( $^~new_glob_patterns(N) ) # (N) for nullglob The bash equivalent would be: glob_patterns=( '*.log' '*.json' ) shopt -s extglob # for +(...) new_glob_patterns=( "${glob_patterns[@ … (log|json)' new_glob_patterns=$glob_pattern'.<->.gz' expanded=( $~new_glob_pattern(N) ) # (N) for nullglob The bash equivalent would be: shopt -s extglob # for @(...) glob_pattern='*. …
Stéphane Chazelas's user avatar
7 votes

How to do "if variable a has pattern 'abc' do x, otherwise do y" (in 1 line)

; esac You're getting an error, because spaces are missing after [[ and because -z is to test if a particular string is empty. [[ is a non-standard feature, how it behaves depends on the version of bash … The case structure is POSIX and as written will work in any POSIX shell and any version of bash. …
Stéphane Chazelas's user avatar
102 votes

How do I automatically answer y in bash script?

That's what the yes command is for. It outputs ys one per line indefinitely so it can be piped to commands that ask yes/no questions. yes | /opt/MNG/MNGVIEWHP/fe/uninstall That answers y to all que …
Stéphane Chazelas's user avatar
57 votes
Accepted

Setting IFS for a single statement

In some shells (including bash): IFS=: command eval 'p=($PATH)' (with bash, you can omit the command if not in sh/POSIX emulation). …
Stéphane Chazelas's user avatar

1
2 3 4 5
33
15 30 50 per page