Hot answers tagged

17

That's because in <<< $line, bash does word splitting, (though not globbing) on $line as it's not quoted there and then joins the resulting words with the space character (and puts that in a temporary file followed by a newline character and makes that the stdin of cut). $ a=a,b,,c bash -c 'IFS=","; sed -n l <<< $a' a b c$ tab happens ...


13

What happens is that bash replaces the tabs with spaces. You can avoid this problem by saying "$line" instead, or by explicitly cutting on spaces.


9

The problem is that you're not quoting $line. To investigate, change the two scripts so they simply print $line: #!/usr/bin/env bash while read line; do echo $line done < "$1" and #!/usr/bin/env zsh while read line; do echo $line done < "$1" Now, compare their output: $ bash.sh input foo bar baz foo bar baz $ zsh.sh input foo bar ...


5

It isn't an apostrophe in the filename. The filenames that have whitespaces are getting 'wrapped'.


4

feh is an image viewer, just ignore that part... you want just the second part. Basically, to access a random array element you want something like ${arr[${ri}]} where ri is $(( $RANDOM % ${#arr[@]} + 1)) that is, ri is a random index of the array arr Now, $RANDOM % N resolves to a random number from 0 to N-1. In this case N is the array length ${#arr[@...


3

Use pgrep like this: if ! pgrep -u $USER top >/dev/null 2>&1; then exec top fi Explanation: The pgrep utility exits with a zero exit code if the command can be found running. Therefore, negate the test. Also restrict it to the current user with -u $USER, so that it doesn't for some reason pick up someone else's processes. $USER is a ...


3

I propose to use a function instead: mygulp () { trap : INT; gulp || say "Gulp has crashed"; } The trap will catch Ctrl-C ("interrupt" signal) and run : command (ie. nothing). Notice also || instead of ; - this way if gulp exits normally (you didn't press ctrl-c) then nothing happens, but if exit code is not zero then say command will start. This ...


3

The typical way to run-some-command-on-the-changing-of-the-directory is via the chpwd hook function (or list of functions named in the appropriately named chpwd_functions array): % function chpwd () { pwd } % cd ~/tmp /Users/jdoe/tmp % pushd /etc /etc ~/tmp /etc % chpwd_functions=( chpwd_do_ls ) % function chpwd_do_ls () { ls } % cd / / Applications ...


2

emacs-mode and vi-mode are only different presets of key bindings. Any widget can be bound in any mode. The widgets in question here are history-beginning-search-backward (presumably for ↑) and history-beginning-search-forward (for ↓). To bind them to the up and down keys in vicmd mode, you just need to run bindkey '^[[A' history-beginning-search-backward ...


2

Inside the ZSH completion system, a custom message upon mashing of tab for a particular argument of a command is possible via the _message function, as shown in this completion script (_foo) for a particular command (foo): #compdef foo local curcontext="$curcontext" state _arguments -C -s \ '1: :->dofoo' \ && return 0 case "$state" in ...


2

First of all, I can’t reproduce the results you claim for the command you showed.  I got the files being renamed to another directory_file1.jpg, another directory_file2.jpg, etc., but still under the some directory directories. Secondly, because of the depth of your directory structure, you should be using -mindepth 4 instead of 2.  Thirdly, I strongly ...


2

If the program is always started from the shell and you have full control over it (ie. it is not hardcoded inside 3rd party program), then you can use some locking mechanism like lockfile or flock. For example by adding flock -n /tmp/top -c top to the .bashrc/.zshrc you will ensure that only first instance of the shell will run the top command (unless you ...


2

As a rule, shells just execute the commmands they're given, they don't automatically save their state. It's common to have different state in different shell instances (e.g. settings depending on the current directory). While it isn't very common to have different sets of key bindings, it can be useful (for example with different keyboards when accessing the ...


2

The quick answer, put this in your ~/.zshrc: bindkey -v '^?' backward-delete-char Normally '^?' (backspace) is bound to vi-backward-delete-char, which is supposed to have the behavior you observe, as that is how vi (not vim, unless in vi classic mode) behaves.


2

How to print all parameters starting with foo: declare -p ${(Mk)parameters:#foo*} All variables are stored in the parameters associative array, so it is enough to print all its keys (k) and search for pattern foo*. The flag (M) is present to remove non-matching elements. Output: typeset foo_a=1 typeset foo_b=2 (notice that declare is the same as ...


1

Your .zshrc line should be export PS1='%n>'


1

The error message is at least consistent with a failed attempt to run a command directly through ssh, e.g. ssh someuser@somehost somecommand. Depending on how zsh is started and in which configuration file you put the alias by the app it is possible that it was just not loaded. Also, the history file will only be written by interactive sessions (which is a "...


1

It won't take the functions raw. They need to be wrapped in a "widget" by doing zle -N widgetname funcname The two can have the same name: zle -N hw{,} Then it's possible to do: bindkey ^h hw , causing Ctrl+h to run the hw widget which runs the hw function.


1

You can bind completion to left arrow with bindkey '^[[D' expand-or-complete where ^[[D represents left arrow (run Ctrl+V or cat and hit arrow key to test that). If you also want to unbind Tab key then bindkey -r '^I'


1

print subprocess.check_output(['zsh','-i','-c','echo $PS1']) When running zsh from another shell, the quotes around echo $PS1 are necessary so that the whole string is passed as the single argument after -c. They had to be single quotes because in double-quotes, the first shell would have expanded $PS1. In Python, the whole command is a single string, so ...


1

I think the correct way is to call subprocess as follows: >>> prompt = subprocess.check_output("""echo $PS1""",shell=True,executable="/bin/zsh") Then you can check the result by >>> prompt Enter or you can use call to see the results directly: >>> subprocess.call("""echo $PS1""",shell=True,executable="/bin/zsh") So you don't ...


1

As has already been answered, a more portable way to use a variable is to quote it: $ printf '%s\t%s\t%s\n' foo bar baz foo bar baz $ l="$(printf '%s\t%s\t%s\n' foo bar baz)" $ <<<$l sed -n l foo bar baz$ $ <<<"$l" sed -n l foo\tbar\tbaz$ There is a difference of implementation in bash, with the line: l="$(printf '%s\t%s\t%...


1

Agreeing that pgrep is the way to go, there are some pitfalls. You need not use the -q option, since you can redirect the output of pgrep to eliminate that: pgrep -U $USER top >/dev/null || exec top The pitfalls lie in the fact that it is not a standard utility, so you have to keep in mind that it may not be portable. Here are a few manpage links, to ...


1

The key bindings shown are for xterm (and programs which copy xterm's behavior). urxvt is a descendant of rxvt, which uses different key bindings. By default, controlleft for rxvt/urxvt sends escapeOd. You can see this by running cat -v and typing that key combination. It will echo as ^[Od. The same issue applies to the other cursor keys. urxvt ...



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