Pipe output to environment variable export command

Mark Unsworth

I'm trying to set a git hash value into an environment variable, i thought it would be as simple as doing this:

git log --oneline -1 | export GIT_HASH=$1

But the $1 doesn't contain anything. What am I doing wrong?

Don Cruickshank

$1 is used to access the first argument in a script or a function. It is not used to access output from an earlier command in a pipeline.

You can use command substitution to get the output of the git command into an environment variable like this:

export GIT_HASH=`git log --oneline -1`

However...

This answer is specially in response to the question regarding the Bourne Shell and it is the most widely supported. Your shell (e.g. GNU Bash) will most likely support the $() syntax and so you should also consider Michael Rush's answer.

But some shells, like tcsh, do not support the $() syntax and so if you're writing a shell script to be as bulletproof as possible for the greatest number of systems then you should use the `` syntax despite the limitations.

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

From Dev

pipe output to stdout and then to command then to variable

From Dev

How do I pipe the output of a curl command to an environment variable and use it in another curl command?

From Dev

Pipe Bash command output to stdout and to a variable

From Java

Setting an environment variable before a command in Bash is not working for the second command in a pipe

From Dev

Save output of command that modifies environment into a variable

From Dev

How to create an environment variable that is the output of a command

From Dev

windows oneliner to set a command output in an environment variable

From Dev

Pipe in Environment variable in fish shell doesn't execute the first command?

From Dev

pipe output into a delete command

From Dev

Pipe the output of a command if it is successful

From Dev

pipe output into a delete command

From Dev

pipe command output to convert?

From Dev

Set environment variable to output of command in Windows command prompt

From Dev

Pipe the output of a command to rm command

From Dev

How to pass export variable with two find command output in shell script

From Dev

Bash time command output is different with environment variable assignment

From Dev

How to pipe output to sh script and pipe that to a command?

From Dev

Zsh pipe all output to command

From Dev

Passing pipe output to Test command

From Dev

Pipe command to suppress output on success

From Dev

Pipe Ipython magic output to a variable?

From Dev

Pipe command output to input of running/backgrounded command

From Dev

Save command output as environment variables

From Dev

Expand Environment Variable from PIPE (SHELL)

From Dev

bash securely pipe environment variable to stdin

From Dev

Assigning the output of a command to a variable

From Dev

Set output of a command to a variable

From Dev

curl command output in a variable

From Dev

Set command output as variable

Related Related

  1. 1

    pipe output to stdout and then to command then to variable

  2. 2

    How do I pipe the output of a curl command to an environment variable and use it in another curl command?

  3. 3

    Pipe Bash command output to stdout and to a variable

  4. 4

    Setting an environment variable before a command in Bash is not working for the second command in a pipe

  5. 5

    Save output of command that modifies environment into a variable

  6. 6

    How to create an environment variable that is the output of a command

  7. 7

    windows oneliner to set a command output in an environment variable

  8. 8

    Pipe in Environment variable in fish shell doesn't execute the first command?

  9. 9

    pipe output into a delete command

  10. 10

    Pipe the output of a command if it is successful

  11. 11

    pipe output into a delete command

  12. 12

    pipe command output to convert?

  13. 13

    Set environment variable to output of command in Windows command prompt

  14. 14

    Pipe the output of a command to rm command

  15. 15

    How to pass export variable with two find command output in shell script

  16. 16

    Bash time command output is different with environment variable assignment

  17. 17

    How to pipe output to sh script and pipe that to a command?

  18. 18

    Zsh pipe all output to command

  19. 19

    Passing pipe output to Test command

  20. 20

    Pipe command to suppress output on success

  21. 21

    Pipe Ipython magic output to a variable?

  22. 22

    Pipe command output to input of running/backgrounded command

  23. 23

    Save command output as environment variables

  24. 24

    Expand Environment Variable from PIPE (SHELL)

  25. 25

    bash securely pipe environment variable to stdin

  26. 26

    Assigning the output of a command to a variable

  27. 27

    Set output of a command to a variable

  28. 28

    curl command output in a variable

  29. 29

    Set command output as variable

HotTag

Archive