help-gawk
[Top][All Lists]
Advanced

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

Re: Tilde expansion in awk pipes


From: Neil R. Ormos
Subject: Re: Tilde expansion in awk pipes
Date: Sat, 5 Oct 2024 19:10:54 -0500 (CDT)
User-agent: Alpine 2.20 (DEB 67 2015-01-07)

limited time wrote:

> Hello, there seem to be some situations where
> tilde expansion does not work as usual.

> [...]

> The set -x command turns on xtracing in zsh and
> it shows that awk is quoting the tilde sign.

> Is this by design ? [...]

I believe the behavior you are seeing is a consequence of gawk calling a shell 
other than 'zsh' when you use the pipe operator.  (The system() built-in 
function works similarly.)  And that shell very likely has tilde expansion 
rules that are different from those of zsh.

On my system, gawk calls the 'sh' shell, and '/bin/sh' is symlinked to the 
'dash' shell.  Yours may be different.

==========

> [reordered] All three forms work in the interactive shell :-

>> echo ~'/path/to/file' ; echo ~'' ; echo ~"/path/to/file"

> /Users/username/path/to/file
> /Users/username
> /Users/username/path/to/file

When I ran

  echo ~'/path/to/file' ; echo ~'' ; echo ~"/path/to/file"

in each of 'dash' and 'bash', the results matched the results you posted from 
gawk.

==========

If you are using a GNU/Linux system, the following command might show you what 
command gawk is running when you use the pipe-to-getline operation:

  gawk 'BEGIN{ while (fres=("ps -o pid,ppid,tty,args" | getline rec)) { print 
rec };  }'

(That should be on a single line.  In the output, there should be two lines 
containing the 'ps ...' command; the longer of the two will indicate the 
calling shell.)

==========

If your external command really has to run in 'zsh' instead of 'sh', you can 
nest the command within an explicit call to 'zsh', but very careful attention 
to quoting or escaping the arguments will be required.



reply via email to

[Prev in Thread] Current Thread [Next in Thread]