Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Implement $(< file) #273

Closed
Crestwave opened this issue Mar 28, 2019 · 8 comments
Closed

Implement $(< file) #273

Crestwave opened this issue Mar 28, 2019 · 8 comments

Comments

@Crestwave
Copy link
Contributor

This is basically equivalent to $(cat file) in Bash but with builtins.

@andychu
Copy link
Contributor

andychu commented Mar 28, 2019

Hm it looks like this case triggers a bug:

osh$ $(< INSTALL.txt)
Warning: SimpleCommand or Assignment has no location information

For some reason completion also doesn't work after <, but it does after cat <.

@jyn514
Copy link
Contributor

jyn514 commented Apr 20, 2019

I can reproduce this without command substitution: > f. This should create (and possibly overwrite) fexactly as if run with c > f where c has no output.

@andychu
Copy link
Contributor

andychu commented Jun 27, 2019

OK I fixed the location error.

I'm not sure about $(< file) being like cat though. dash doesn't implement it, but bash, mksh, and zsh do.

Is it commonly used in scripts, or does POSIX require it?

@Crestwave
Copy link
Contributor Author

OK I fixed the location error.

I'm not sure about $(< file) being like cat though. dash doesn't implement it, but bash, mksh, and zsh do.

Is it commonly used in scripts, or does does POSIX require it?

No, it's not POSIX. It's used to read a file by people who know of it; e.g., Neofetch uses it.

@andychu
Copy link
Contributor

andychu commented Aug 30, 2019

@Crestwave Crestwave mentioned this issue Apr 6, 2020
@andychu
Copy link
Contributor

andychu commented Apr 7, 2020

An interesting thing about this is that zsh supports an extended version of it. I think bash is just literally special casing $(< file) to mean something different?

echo $(<file)  # works in both
echo $(< file; echo hi)      # zsh respects the meaning of < file, but bash doesn't

Also, zsh extends the meaning to process subs, like

cat < <(<file; echo hi)       # a confusing way of doing it, but it works

@andychu
Copy link
Contributor

andychu commented Apr 7, 2020

And it also doesn't support pipelines and subshells:

(< file)    # doesn't cat the file

< file | wc -l  # doesn't work, nothing goes to stdout

andychu pushed a commit that referenced this issue Apr 7, 2020
This is a curious construct described in issue #273.  It works in
multiple shells.

Also fix lint error, to fix the build.
andychu pushed a commit that referenced this issue Apr 7, 2020
Internally, $(< file) is rewritten to

$(__cat < file)

where __cat is a builtin like the external 'cat'.

Addresses issue #273.
@andychu andychu closed this as completed Apr 19, 2020
@bb010g
Copy link
Contributor

bb010g commented Feb 12, 2021

And it also doesn't support pipelines and subshells:

See Zsh's NULLCMD options & section "Redirections with no command" of zshmisc(1).

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

4 participants