Bash parameter substituiton within commands
Clash Royale CLAN TAG#URR8PPP
up vote
1
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
1
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
1
down vote
favorite
up vote
1
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 1 hour ago
Pureferret
4431819
4431819
add a comment |Â
add a comment |Â
2 Answers
2
active
oldest
votes
up vote
3
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=$ ... )":2:4
add a comment |Â
up vote
1
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
3
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=$ ... )":2:4
add a comment |Â
up vote
3
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=$ ... )":2:4
add a comment |Â
up vote
3
down vote
accepted
up vote
3
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=$ ... )":2:4
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=$ ... )":2:4
answered 40 mins ago
jimmij
29.4k867101
29.4k867101
add a comment |Â
add a comment |Â
up vote
1
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
1
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
1
down vote
up vote
1
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 9 mins ago
Stéphane Chazelas
287k53529867
287k53529867
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