help-bash
[Top][All Lists]
Advanced

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

Re: i vote for $( .. <file ) be available also as non first command in s


From: Alex fxmbsw7 Ratchev
Subject: Re: i vote for $( .. <file ) be available also as non first command in subshells ( and maybe process subst ) ?
Date: Fri, 12 Mar 2021 15:00:32 +0100

thank you firstly for your too big mail :))

i wasnt doing much, i was doing PS0=$( <file ) PS1=$( <other )
PROMPT_COMMAND=$( <third ) where the files were optional and i wanted to
supress it

i guess, the brackets way you presented ill choose

{ var=$( <nonexist ) ; } 2>&-

gotta think more

well sorry for shorter mail but thank you big time still

*thinking*

On Fri, Mar 12, 2021 at 2:13 PM Greg Wooledge <greg@wooledge.org> wrote:

> On Fri, Mar 12, 2021 at 10:17:58AM +0100, Alex fxmbsw7 Ratchev wrote:
> > .. :)
>
> Please don't put the content of your message in the Subject: header.
> The Subject: header may be lost, changed, truncated, ignored, etc.
>
> In the interest of readability, let's copy it down here into the body:
>
> > i vote for $( .. <file ) be available also as non first command
> > in subshells ( and maybe process subst ) ?
>
> OK, first things first: $(<file) is special syntax.  It is defined to
> be equivalent to $(cat file), except that it doesn't need to exec cat(1),
> so it is marginally more efficient.
>
> As you observed in IRC, however, it is not the same:
>
> x=$(cat nosuchfile 2>/dev/null)
> x=$(<nosuchfile 2>/dev/null)
>
> These two commands are not equivalent.  In the first one, the error message
> is suppressed.  In the second, it is printed.  For some reason, you
> thought this was important enough to complain about.
>
> If you don't like the behavior of $(<nosuchfile) then simply don't use
> it.  Use cat instead.  Or wrap the assignment in a curly-brace block
> and move the 2>/dev/null.
>
> { x=$(<nosuchfile); } 2>/dev/null
>
> All of them fork a new process, so the $(<) form is only a *tiny* bit
> more efficient, not a lot more efficient.  You can simply stop using it.
>
> Second thing second: outside of the special $(< syntax, <file is a
> redirection.  If you write a redirection without a command, the behavior
> is well known.  A standalone output redirection is used to truncate a
> file -- the shell opens the file for writing (which truncates it), does
> nothing, and then closes the file.  People use this all the time.
>
> A standalone input redirection doesn't really serve any purpose.  The
> shell will open the file for reading, do nothing, and then close the
> file.  I suppose you could use this to verify that a file is truly
> readable, but most people would simply use test -r.
>
> Third thing third: $( ) is a command substitution.  Not a subshell, and
> not a process substitution.  A subshell is ( ), and a process
> substitution is <( ) or >( ).
>
> (Yes, technically, all three of them create subshells, but come on, you
> KNOW that an explicit subshell is ( ) not $( ).  You know better.  Stop
> using the wrong names for things.)
>
> > the story is for me i think, $( < file ) is useful, more useful advanced
> > core even more useful, but then i cant do <file anymore to print its
> output
> > which would also be handy in <( >( ..
>
> I have no idea what "more useful advanced core even more useful" means.
>
> Apart from  x=$(<nosuchfile 2>/dev/null)  which you didn't even mention
> in the email -- only in IRC -- what exactly are you trying to do?  Show
> us an example or something.
>
> One of the other things you showed in IRC (why oh why can't you show them
> in your email???) was this:
>
> x=$(foo; bar; <file)
>
> Apparently you thought this would be useful for some reason.  However,
> this is never going to be equivalent to
>
> x=$(foo; bar; cat file)
>
> As mentioned previously, a standalone redirection already has a meaning.
> You can't just "vote" to redefine it to mean "pretend I wrote cat".  The
> special form $(<file) is special.  It's a one-time-only thing.  It's
> a new piece of syntax that consists of three characters, but you're allowed
> to put whitespace between the second and third character.
>
> You can't just use a PIECE of this syntax in a different context.
>
> If you don't have the three characters $(< in a row (with optonal
> whitespace
> allowed in ONE position only), then you are not using this feature.  You
> are doing something else.
>
>


reply via email to

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