Zsh Mailing List Archive
Messages sorted by: Reverse Date, Date, Thread, Author

Re: Filename Expansion



On Sun, Oct 6, 2024, at 3:46 PM, limited time wrote:
> The reason I stumbled upon this issue in the first place is perhaps it 
> might be related to this awk script not behaving as expected :-
> BEGIN {
>   "set -x ; echo ~'/path/to/file'" | getline output1
>   "set -x ; echo ~''" | getline output2
>   "set -x ; echo ~\"/path/to/file\"" | getline output3
>
>   print output1
>   print output2
>   print output3
> }
>
> Running this script :-
>
> ❯ gawk -f script.awk
> + echo '~/path/to/file'
> + echo '~'
> + echo '~/path/to/file'
> ~/path/to/file
> ~
> ~/path/to/file
> No tilde expansion occurs here as would in an interactive shell or a script.
>
>
> You can follow the discussion on the script here if you wish to : 
> https://lists.gnu.org/archive/html/help-gawk/2024-10/index.html

For

	cmd | getline

awk interprets "cmd" with sh.  As you say in the help-gawk thread,
your system's sh is a link to zsh.  When invoked via that link, zsh
runs in sh compatibility mode [1], which enables (among other things)
the SH_FILE_EXPANSION option [2]:

	% cat /tmp/tilde.zsh
	print -lr ~'/path/to/file' ~"" ~"/path/to/file"
	% zsh /tmp/tilde.zsh
	/Users/foo/path/to/file
	/Users/foo
	/Users/foo/path/to/file
	% zsh -o SH_FILE_EXPANSION /tmp/tilde.zsh
	~/path/to/file
	~
	~/path/to/file

This compatibility behavior is specified by POSIX [3] and aligns
with bash 5.2.32, dash 0.5.12, mksh R59 2020/10/31, OpenBSD 7.5
pdksh, and yash 2.56.1 (but not ksh93u+ 2012-08-01).

Since awk uses sh to interpret shell commands, it's a good idea to
write said commands portably, eschewing shell-specific behaviors.

[1]: https://zsh.sourceforge.io/Doc/Release/Invocation.html#Compatibility
[2]: https://zsh.sourceforge.io/Doc/Release/Options.html#index-SH_005fFILE_005fEXPANSION
[3]: https://pubs.opengroup.org/onlinepubs/9799919799/utilities/V3_chap02.html#tag_19_06_01

-- 
vq




Messages sorted by: Reverse Date, Date, Thread, Author