Skip to main content
deleted 169 characters in body
Source Link
mikeserv
  • 59.4k
  • 10
  • 123
  • 244
  • Variables

  • These are portably defined with a: name=value command outside of a listlists context.

  • These may also be defined with some builtins such as export, set, readonly in some cases.

  • Functions

  • These are portably defined with: name() compound command <>optionalcompound_command redirect<>any_redirects command in command position.

  • The shell is specified not to expand or interpret any portion of the function definition it might find in the command at definition time.

    • This prohibition does not include aliases, though, as these are expanded during the parsing process of a shell's command read, and so alias values, if the alias is found in correct context, are expanded into the function definition command when found.
  • As mentioned, the shell saves the parsed value of the definition command as a static string in its memory, and it performs all expansions and redirections found within the string only when the function name is later found post-parse as called.

  • Aliases

  • These are portably defined as arguments to the alias builtin with a: alias name=value command.in command position

  • Like functions, alias definitions are interpreted when the definition names are later found in command position.

  • Unlike functions, though, as their definitions are arguments to the alias command, valid shell expansions found within are also interpreted at define time. And so alias definitions are always twice interpreted.

  • Also unlike functions, alias definitions are not parsed at all at define time, but, rather, it is the shell's parser that expands their values pre-parse into a command string when they are found.

  • Variables

  • These are portably defined with a name=value command outside of a list context.

  • These may also be defined with some builtins such as export, set, readonly in some cases.

  • Functions

  • These are portably defined with name() compound command <>optional redirect command in command position.

  • The shell is specified not to expand or interpret any portion of the function definition it might find in the command at definition time.

    • This prohibition does not include aliases, though, as these are expanded during the parsing process of a shell's command read, and so alias values, if the alias is found in correct context, are expanded into the function definition command when found.
  • As mentioned, the shell saves the parsed value of the definition command as a static string in its memory, and it performs all expansions and redirections found within the string only when the function name is later found post-parse as called.

  • Aliases

  • These are portably defined as arguments to the alias builtin with a alias name=value command.

  • Like functions, alias definitions are interpreted when the definition names are later found in command position.

  • Unlike functions, though, as their definitions are arguments to the alias command, valid shell expansions found within are also interpreted at define time. And so alias definitions are always twice interpreted.

  • Also unlike functions, alias definitions are not parsed at all at define time, but, rather, it is the shell's parser that expands their values pre-parse into a command string when they are found.

  • Variables: name=value outside of lists

  • These may also be defined with some builtins such as export, set, readonly in some cases.

  • Functions: name() compound_command <>any_redirects in command position

  • The shell is specified not to expand or interpret any portion of the function definition it might find in the command at definition time.

    • This prohibition does not include aliases, though, as these are expanded during the parsing process of a shell's command read, and so alias values, if the alias is found in correct context, are expanded into the function definition command when found.
  • As mentioned, the shell saves the parsed value of the definition command as a static string in its memory, and it performs all expansions and redirections found within the string only when the function name is later found post-parse as called.

  • Aliases: alias name=value in command position

  • Like functions, alias definitions are interpreted when the definition names are later found in command position.

  • Unlike functions, though, as their definitions are arguments to the alias command, valid shell expansions found within are also interpreted at define time. And so alias definitions are always twice interpreted.

  • Also unlike functions, alias definitions are not parsed at all at define time, but, rather, it is the shell's parser that expands their values pre-parse into a command string when they are found.

added 984 characters in body
Source Link
mikeserv
  • 59.4k
  • 10
  • 123
  • 244
  • Variables

  • These are portably defined with a name=value command outside of a list context.

  • These may also be defined with some builtins such as export, set, readonly in some cases.

  • Functions

  • These are portably defined with name() compound command <>optional redirect command in command position.

  • The shell is specified not to expand or interpret any portion of the function definition it might find in the command at definition time.

    • This prohibition does not include aliases, though, as these are expanded during the parsing process of a shell's command read, and so alias values, if the alias is found in correct context, are expanded into the function definition command when found.
  • As mentioned, the shell saves the parsed value of the definition command as a static string in its memory, and it performs all expansions and redirections found within the string only when the function name is later found post-parse as called.

  • Aliases

  • These are portably defined as arguments to the alias builtin with a alias name=value command.

  • Like functions, alias definitions are interpreted when the definition names are later found in command position.

  • Unlike functions, though, as their definitions are arguments to the alias command, valid shell expansions found within are also interpreted at define time. And so alias definitions are always twice interpreted.

  • Also unlike functions, alias definitions are not parsed at all at define time, but, rather, it is the shell's parser that expands their values pre-parse into a command string when they are found.

You should always call an alias name on an input line other than the one on which the definition is found - again, this is to do with the parse order of the command. When combined, aliases and functions can be used together to portably, safely, and effectively move information and parameter arrays in and out of subshelled contexts.


Almost unrelated, but here's another fun one:

alias mlinesh='"${SHELL:-sh}" <<"" '

Run that at an interactive prompt and any arguments you pass to it on the same execution line as the line on which you call it will be interpreted as arguments to a subshell. All lines thereafter until the first occurring blank line, though, are read-in by the current shell to pass as stdin to the subshell it prepares to call, and none of these are interpreted in any way at all.

$ mlinesh -c '. /dev/fd/0; foo'
> foo(){ echo 'Inside Function'; } 
> 

Inside Function

...but just...

$ mlinesh
> foo(){ echo 'Inside Function'; }
> foo
> 

...is easier...

  • Variables

  • These are portably defined with a name=value command outside of a list context.

  • These may also be defined with some builtins such as export, set, readonly in some cases.

  • Functions

  • These are portably defined with name() compound command <>optional redirect command in command position.

  • The shell is specified not to expand or interpret any portion of the function definition it might find in the command at definition time.

    • This prohibition does not include aliases, though, as these are expanded during the parsing process of a shell's command read, and so alias values, if the alias is found in correct context, are expanded into the function definition command when found.
  • As mentioned, the shell saves the parsed value of the definition command as a static string in its memory, and it performs all expansions and redirections found within the string only when the function name is later found post-parse as called.

  • Aliases

  • These are portably defined as arguments to the alias builtin.

  • Like functions, alias definitions are interpreted when the definition names are later found in command position.

  • Unlike functions, alias definitions are not parsed at define time, but, rather, it is the shell's parser that expands their values pre-parse into a command string when they are found.

You should always call an alias name on an input line other than the one on which the definition is found - again, this is to do with the parse order of the command. When combined, aliases and functions can be used together to portably, safely, and effectively move information and parameter arrays in and out of subshelled contexts.

  • Variables

  • These are portably defined with a name=value command outside of a list context.

  • These may also be defined with some builtins such as export, set, readonly in some cases.

  • Functions

  • These are portably defined with name() compound command <>optional redirect command in command position.

  • The shell is specified not to expand or interpret any portion of the function definition it might find in the command at definition time.

    • This prohibition does not include aliases, though, as these are expanded during the parsing process of a shell's command read, and so alias values, if the alias is found in correct context, are expanded into the function definition command when found.
  • As mentioned, the shell saves the parsed value of the definition command as a static string in its memory, and it performs all expansions and redirections found within the string only when the function name is later found post-parse as called.

  • Aliases

  • These are portably defined as arguments to the alias builtin with a alias name=value command.

  • Like functions, alias definitions are interpreted when the definition names are later found in command position.

  • Unlike functions, though, as their definitions are arguments to the alias command, valid shell expansions found within are also interpreted at define time. And so alias definitions are always twice interpreted.

  • Also unlike functions, alias definitions are not parsed at all at define time, but, rather, it is the shell's parser that expands their values pre-parse into a command string when they are found.

You should always call an alias name on an input line other than the one on which the definition is found - again, this is to do with the parse order of the command. When combined, aliases and functions can be used together to portably, safely, and effectively move information and parameter arrays in and out of subshelled contexts.


Almost unrelated, but here's another fun one:

alias mlinesh='"${SHELL:-sh}" <<"" '

Run that at an interactive prompt and any arguments you pass to it on the same execution line as the line on which you call it will be interpreted as arguments to a subshell. All lines thereafter until the first occurring blank line, though, are read-in by the current shell to pass as stdin to the subshell it prepares to call, and none of these are interpreted in any way at all.

$ mlinesh -c '. /dev/fd/0; foo'
> foo(){ echo 'Inside Function'; } 
> 

Inside Function

...but just...

$ mlinesh
> foo(){ echo 'Inside Function'; }
> foo
> 

...is easier...

hyphen?
Source Link
mikeserv
  • 59.4k
  • 10
  • 123
  • 244

You've probably noticed what I consider to be the primary difference between an alias or a function above, which is the shell's expansion point for each. The differences, actually, can make their combination quite useful.

alias foo='
    foo(){
        printf "Inside Function: %s\n" "$@"
        unset -f foo
    };  foo "$@" '

That is an alias that will define a function that unsets itself when called, and so it affects one namespace by the application of another. It then calls the function. The example is naive; it isn't very useful in a regular context, but the following example might demonstrate some other limited usefulness it might offer in other contexts:

sh -c "
    alias $(alias foo)
    foo \'
" -- \; \' \" \\

Inside Function: ;
Inside Function: '
Inside Function: "
Inside Function: \
Inside Function: '

You should always call an alias name on an input line other than the one on which the definition is found - again, this is to do with the parse order of the command. When combined, aliases and functions can be used together to portably, safely, and effectively move information and parameter arrays in and out of subshelled contexts.

You've probably noticed what I consider to be the primary difference between an alias or a function above, which is the shell's expansion point for each. The differences, actually, can make their combination quite useful.

alias foo='
    foo(){
        printf "Inside Function: %s\n" "$@"
        unset -f foo
    };  foo "$@" '

That is an alias that will define a function that unsets itself when called, and so it affects one namespace by the application of another. It then calls the function. The example is naive; it isn't very useful in a regular context, but the following example might demonstrate some other limited usefulness it might offer in other contexts:

sh -c "
    alias $(alias foo)
    foo \'
" -- \; \' \" \\

Inside Function: ;
Inside Function: '
Inside Function: "
Inside Function: \
Inside Function: '

You should always call an alias name on an input line other than the one on which the definition is found - again, this is to do with the parse order of the command. When combined, aliases and functions can be used together to portably, safely, and effectively move information and parameter arrays in and out of subshelled contexts.

hyphen?
Source Link
mikeserv
  • 59.4k
  • 10
  • 123
  • 244
Loading
hyphen?
Source Link
mikeserv
  • 59.4k
  • 10
  • 123
  • 244
Loading
Source Link
mikeserv
  • 59.4k
  • 10
  • 123
  • 244
Loading