The second line of the script only works if I trigger glob expansion by executing echo
. I can't understand why. Here's the command and it's execution to give some context.
Function definition:
~/ cat ~/.zsh/includes/ascii2gif
ascii2gif () {
setopt extendedglob
input=$(echo ${1}(:a))
_path=${input:h}
input_f=${input:t}
output_f=${${input_f}:r}.gif
cd $_path
nerdctl run --rm -v $_path:/data asciinema/asciicast2gif -s 2 -t solarized-dark $input_f $output_f
}
Activate function debugging for ascii2gif function..
~/ typeset -f -t ascii2gif
Debugged function execution:
~/ ascii2gif ./demo.cast
+ascii2gif:1> input=+ascii2gif:1> echo /Users/b/demo.cast
+ascii2gif:1> input=/Users/b/demo.cast
+ascii2gif:2> _path=/Users/b
+ascii2gif:3> input_f=demo.cast
+ascii2gif:4> output_f=demo.gif
+ascii2gif:5> cd /Users/b
+_direnv_hook:1> trap -- '' SIGINT
+_direnv_hook:2> /Users/b/homebrew/bin//direnv export zsh
+_direnv_hook:2> eval ''
+_direnv_hook:3> trap - SIGINT
+ascii2gif:6> nerdctl run --rm -v /Users/b:/data asciinema/asciicast2gif -s 2 -t solarized-dark demo.cast demo.gif
==> Loading demo.cast...
==> Spawning PhantomJS renderer...
==> Generating frame screenshots...
==> Combining 40 screenshots into GIF file...
==> Done.
I've tried numerous variations to try and force expansion such as input=${~1}(:a)
etc, but to no avail. Any suggestions? Obviously the script works but seems sub-optimal.
ascii2gif () {
?~/ cat ~/.zsh/includes/ascii2gif
does?setopt
and variable assignments have a global effect by default. When you change options for the purposes of a function, either usesetopt local_options
or, usually, useemulate -L zsh
so that your function will work if some options you weren't thinking about have been changed from the default. Also make variables local withlocal
(ortypeset
). Also it's probably a bad idea to change the current directory in this function. Or you could run the whole function in a subshell by putting(…)
around the body instead of{…}
(but still keepemulate zsh
).