Bash parameter substituiton within commands
Clash Royale CLAN TAG#URR8PPP
up vote
3
down vote
favorite
I have a passing understanding of Parameter Substitution, including substrings like so
foo="Hello World";
greeting=$foo:0:6
But do I do this with commands?
greeting="Hello"
md5greeting=$(echo $greeting | md5sum :0:6)
>>b1946a
Where the output is the first 6 characters of the md5sum of the 'hello'.
How do I achieve this?
shell-script command-substitution variable-substitution
add a comment |Â
up vote
3
down vote
favorite
I have a passing understanding of Parameter Substitution, including substrings like so
foo="Hello World";
greeting=$foo:0:6
But do I do this with commands?
greeting="Hello"
md5greeting=$(echo $greeting | md5sum :0:6)
>>b1946a
Where the output is the first 6 characters of the md5sum of the 'hello'.
How do I achieve this?
shell-script command-substitution variable-substitution
1
echo "$greeting" | md5sum | cut -c 1-6
would work if you know the output will only be a single line.
â glenn jackman
Oct 4 at 14:37
add a comment |Â
up vote
3
down vote
favorite
up vote
3
down vote
favorite
I have a passing understanding of Parameter Substitution, including substrings like so
foo="Hello World";
greeting=$foo:0:6
But do I do this with commands?
greeting="Hello"
md5greeting=$(echo $greeting | md5sum :0:6)
>>b1946a
Where the output is the first 6 characters of the md5sum of the 'hello'.
How do I achieve this?
shell-script command-substitution variable-substitution
I have a passing understanding of Parameter Substitution, including substrings like so
foo="Hello World";
greeting=$foo:0:6
But do I do this with commands?
greeting="Hello"
md5greeting=$(echo $greeting | md5sum :0:6)
>>b1946a
Where the output is the first 6 characters of the md5sum of the 'hello'.
How do I achieve this?
shell-script command-substitution variable-substitution
shell-script command-substitution variable-substitution
asked Oct 4 at 10:35
Pureferret
4531819
4531819
1
echo "$greeting" | md5sum | cut -c 1-6
would work if you know the output will only be a single line.
â glenn jackman
Oct 4 at 14:37
add a comment |Â
1
echo "$greeting" | md5sum | cut -c 1-6
would work if you know the output will only be a single line.
â glenn jackman
Oct 4 at 14:37
1
1
echo "$greeting" | md5sum | cut -c 1-6
would work if you know the output will only be a single line.â glenn jackman
Oct 4 at 14:37
echo "$greeting" | md5sum | cut -c 1-6
would work if you know the output will only be a single line.â glenn jackman
Oct 4 at 14:37
add a comment |Â
2 Answers
2
active
oldest
votes
up vote
5
down vote
accepted
Parameter substitution doesn't work like that, at least not in bash. You need a real variable (parameter) and directly operate on it. So for example like that:
var=$(echo abcdefgh | cmd1 | cmd2 | ... )
var2="$var:2:4"
The exception is zsh
where you can convert command substitution to parameter "on the fly" and do above in one line:
var2=$ cdm1
add a comment |Â
up vote
4
down vote
Note that:
echo $greeting | md5sum
Or more reliably:
printf '%sn' "$greeting" | md5sum
Does not give you the MD5 hash of the content of the variable, but of the content of the variable followed by a newline character. For the MD5 hash of the content of the variable:
printf %s "$greeting" | md5sum
For the first 6 bytes of it, with some head
implementations:
printf %s "$greeting" | md5sum | head -c 6
or more portably:
printf %s "$greeting" | md5sum | dd bs=1 count=6 2> /dev/null
add a comment |Â
2 Answers
2
active
oldest
votes
2 Answers
2
active
oldest
votes
active
oldest
votes
active
oldest
votes
up vote
5
down vote
accepted
Parameter substitution doesn't work like that, at least not in bash. You need a real variable (parameter) and directly operate on it. So for example like that:
var=$(echo abcdefgh | cmd1 | cmd2 | ... )
var2="$var:2:4"
The exception is zsh
where you can convert command substitution to parameter "on the fly" and do above in one line:
var2=$ cdm1
add a comment |Â
up vote
5
down vote
accepted
Parameter substitution doesn't work like that, at least not in bash. You need a real variable (parameter) and directly operate on it. So for example like that:
var=$(echo abcdefgh | cmd1 | cmd2 | ... )
var2="$var:2:4"
The exception is zsh
where you can convert command substitution to parameter "on the fly" and do above in one line:
var2=$ cdm1
add a comment |Â
up vote
5
down vote
accepted
up vote
5
down vote
accepted
Parameter substitution doesn't work like that, at least not in bash. You need a real variable (parameter) and directly operate on it. So for example like that:
var=$(echo abcdefgh | cmd1 | cmd2 | ... )
var2="$var:2:4"
The exception is zsh
where you can convert command substitution to parameter "on the fly" and do above in one line:
var2=$ cdm1
Parameter substitution doesn't work like that, at least not in bash. You need a real variable (parameter) and directly operate on it. So for example like that:
var=$(echo abcdefgh | cmd1 | cmd2 | ... )
var2="$var:2:4"
The exception is zsh
where you can convert command substitution to parameter "on the fly" and do above in one line:
var2=$ cdm1
answered Oct 4 at 10:58
jimmij
29.5k867101
29.5k867101
add a comment |Â
add a comment |Â
up vote
4
down vote
Note that:
echo $greeting | md5sum
Or more reliably:
printf '%sn' "$greeting" | md5sum
Does not give you the MD5 hash of the content of the variable, but of the content of the variable followed by a newline character. For the MD5 hash of the content of the variable:
printf %s "$greeting" | md5sum
For the first 6 bytes of it, with some head
implementations:
printf %s "$greeting" | md5sum | head -c 6
or more portably:
printf %s "$greeting" | md5sum | dd bs=1 count=6 2> /dev/null
add a comment |Â
up vote
4
down vote
Note that:
echo $greeting | md5sum
Or more reliably:
printf '%sn' "$greeting" | md5sum
Does not give you the MD5 hash of the content of the variable, but of the content of the variable followed by a newline character. For the MD5 hash of the content of the variable:
printf %s "$greeting" | md5sum
For the first 6 bytes of it, with some head
implementations:
printf %s "$greeting" | md5sum | head -c 6
or more portably:
printf %s "$greeting" | md5sum | dd bs=1 count=6 2> /dev/null
add a comment |Â
up vote
4
down vote
up vote
4
down vote
Note that:
echo $greeting | md5sum
Or more reliably:
printf '%sn' "$greeting" | md5sum
Does not give you the MD5 hash of the content of the variable, but of the content of the variable followed by a newline character. For the MD5 hash of the content of the variable:
printf %s "$greeting" | md5sum
For the first 6 bytes of it, with some head
implementations:
printf %s "$greeting" | md5sum | head -c 6
or more portably:
printf %s "$greeting" | md5sum | dd bs=1 count=6 2> /dev/null
Note that:
echo $greeting | md5sum
Or more reliably:
printf '%sn' "$greeting" | md5sum
Does not give you the MD5 hash of the content of the variable, but of the content of the variable followed by a newline character. For the MD5 hash of the content of the variable:
printf %s "$greeting" | md5sum
For the first 6 bytes of it, with some head
implementations:
printf %s "$greeting" | md5sum | head -c 6
or more portably:
printf %s "$greeting" | md5sum | dd bs=1 count=6 2> /dev/null
answered Oct 4 at 11:28
Stéphane Chazelas
287k53531868
287k53531868
add a comment |Â
add a comment |Â
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
StackExchange.ready(
function ()
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2funix.stackexchange.com%2fquestions%2f473195%2fbash-parameter-substituiton-within-commands%23new-answer', 'question_page');
);
Post as a guest
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
1
echo "$greeting" | md5sum | cut -c 1-6
would work if you know the output will only be a single line.â glenn jackman
Oct 4 at 14:37