2022 Developer Survey is open! Take survey.

Hot answers tagged

13 votes

Different behaviour of subshells and && in bash vs zsh

You can get a similar thing in Bash: $ echo $$ 26328 $ ( /bin/sleep 1234 ) $ ps -o pid,ppid,stat,args -C sleep PID PPID STAT COMMAND 26473 26328 S+ /bin/sleep 1234 What Bash does there, is that ...
user avatar
  • 115k
4 votes

Error while trying to source a .zshrc file

You're missing / Your command source ~.zshrc tries to lookup the home directory for a user called .zshrc. The source command would presumably attempt to execute that user's home directory - and fail. ...
user avatar
  • 89.2k
4 votes
Accepted

zsh: how to get associative array length?

Unless you had run typeset -A test beforehand, that will have assigned elements of a plain array, not an associative array. zsh arrays like the arrays of most shells and languages (the only exception ...
user avatar
4 votes
Accepted

Print exit status code after each command in terminal

Personally, I add %(?..%B(%?%)%b) to my $PROMPT, so that the exit status of the previous command be shown in bold in brackets if it was unsuccessful, which is less intrusive than printing the exit ...
user avatar
3 votes

How to get unix time of 11PM, and 11PM CST tonight using BSD's date command?

You should be able to specify :%M:%S as part of the format string and specify :00:00 as the time % /bin/date -j -f '%H:%M:%S%p %Z' '11:00:00PM GMT' Wed May 25 19:00:00 EDT 2022 The manpage for ...
user avatar
3 votes
Accepted

Run cargo command in subdirectories with user-specified arguments

The shell doesn't mess up with --. Just do: #! /bin/zsh - for toml (**/Cargo.toml(N.)) cargo -v "$@" --manifest-path $toml And call it as: that-script test -- --ignored Using zsh globbing ...
user avatar
3 votes
Accepted

zsh - How do I prepend a string to every element of an array?

A few options: Using the ^ operator which is intended for that (reminiscent of rc's ^): ARRAY=(my_$^ARRAY) Or to make that kind of expansion the default (in the style of rc or fish): set -o ...
user avatar
3 votes

string substitutions: all occurrences

There are many answers, but none of them are comprehensive, so I felt like adding an answer that sums it all up. Lower/Uppercasing If you are trying to uppercase the string, the simplest way is to use:...
user avatar
2 votes

zsh parameter substitution

I've no idea how that's supposed to work. Assignments don't take the $ sign on the left, so $1=$PWD would not be recognized as an assignment, and would instead just expand both variables and then try ...
user avatar
  • 115k
2 votes
Accepted

evaluate terms in zsh without a command

Intercepting a command before it's executed is possible, but that's too late: the command has already been parsed and aliases have been expanded. So for example 2 * 3 would appear as the command not ...
user avatar
2 votes
Accepted

ZSH: How to set an associative array name and content dynamically?

set -A array value1 value2 is the ancient ksh syntax from the early 80s¹ for defining plain arrays. It's not defining associative arrays and is only supported for compatibility with ksh88. The k and ...
user avatar
2 votes

How to automate the process to open 3 shells and run 3 commands?

Not an answer to your main question (I'm a screen user myself, never felt the need to move to tmux), but a comment on: However, since it is time consuming, so I wrote a shell script to do: cd foo/bar ...
user avatar
2 votes

Find recursively minimum one *.flac file, if no *.cue file in same dir print directory name

You can easily do that with zsh globs alone: has() ()(($#)) $REPLY/?*.$1(NDY1) print -rC1 -- **/*(NDFe['has flac && ! has cue']) Remove the Ds if you want to ignore the hidden files/dirs. ...
user avatar
2 votes

Print exit status code after each command in terminal

In zsh, you can use the precmd hook and do something like this (taken from this answer): % PROMPT_COMMAND='printf "Exit code: %s\n" $?' % precmd() { eval "$PROMPT_COMMAND" } % cat ...
user avatar
  • 215k
1 vote

Print exit status code after each command in terminal

You can add any user defined function into precmd_functions: print_status() echo $? precmd_functions+=(print_status) It is executed before every prompt, that means after the command.
user avatar
  • 6,335
1 vote
Accepted

zsh function with fzf selection requires Enter

fzf-change-dirstack () { local dir print -rNC1 -- $dirstack | fzf --read0 --print0 | IFS= read -rd '' dir && cd -- $dir && zle -I } zle -N fzf-change-dirstack ...
user avatar
1 vote

ZSh script to batch process filenames containing spaces and substitute extension

While your problem was that you had the script interpreted by sh instead of zsh, in zsh, you could use zmv here: #! /bin/zsh - autoload zmv zmv -P ffmpeg -o -i './(*).wma' './$1.wav' zmv normally ...
user avatar
1 vote
Accepted

How to compress a simple shell script onto one line?

The initialization of the variables was left out. if ${folders[@]} is empty, the for loop is never entered.
user avatar
  • 42.5k
1 vote
Accepted

Two Enter Keys are behaving Differently

Yes, it seems alacritty is configured by default to send LF (aka \n, ^J) upon keypad enter, and also regardless of whether application keypad mode is enabled (after tput smkx) even though the terminfo ...
user avatar
1 vote

How to get unix time of 11PM, and 11PM CST tonight using BSD's date command?

Since you're tagging with zsh, note that zsh has builtin time parsing and formatting with its strftime builtin in the zsh/datetime module which is a raw interface to the standard strftime() (+ ...
user avatar
1 vote

Rookie Question: Regex in ZSH Script

zsh has some builtin support for regexp matching (both ERE and PCRE), it also has extended glob operators that go beyond regexps. No need to use that antiquated and broken by design expr command (...
user avatar
1 vote

What's a clean way to run a specific command C for each line L of a given file F and then move every L where C(L) ran unsuccesfully?

Using bash, you can loop for the urls and test the curl command, --fail option of curl seems to be good for use inside scripts, see: How to check whether a command such as curl completed without error ...
user avatar
  • 6,335
1 vote
Accepted

How can I suppress *:// from tab completion suggestions for 'open'?

According to your output, open is an executable in your OS X system. So, to modify its completion syntax, you'd need to figure out where the completion function resides and modify it (I have not been ...
user avatar
1 vote

match number range with non significative zeros in zsh

If it's matching you want, as opposed to generating a list of files regardless of whether they exist or not, use the <x-y> glob operator: print -rC1 -- booklet-<79-256>.pdf <79-256> ...
user avatar
1 vote
Accepted

Variable Scope in ZSH: How to access global variables from within function?

Your functions will see the variable that you have set earlier (and exported or not) provided they haven't been set in a subshell or not declared local to some function that has since returned. Your ...
user avatar
1 vote
Accepted

zsh: How to retrieve the previous command line?

zsh offers a plethora of options for retrieving previous command lines from its history. Here’s just a small selection of these: Press Up Arrow to step through previous command lines. Press Alt. to ...
user avatar

Only top scored, non community-wiki answers of a minimum length are eligible