Assign variable using multiple lines
Clash Royale CLAN TAG#URR8PPP
up vote
2
down vote
favorite
I have a function
f()
echo 777
and a variable to which I assign the "return value" of the function.
x=$(f)
Very concise! However, in my real code, the variable and function names are quite a bit longer and the function also eats positional arguments, so that concise line above, gets very long. Since I like to keep things tidy, I would like to break the code above in two lines.
x=
$(f)
Still works! But: keeping things tidy also means respecting the indentation, so that gives something like
if foo
x=
$(f)
fi
which does not work anymore due to whitespaces! Is there a good workaround for this?
bash newlines assignment
add a comment |Â
up vote
2
down vote
favorite
I have a function
f()
echo 777
and a variable to which I assign the "return value" of the function.
x=$(f)
Very concise! However, in my real code, the variable and function names are quite a bit longer and the function also eats positional arguments, so that concise line above, gets very long. Since I like to keep things tidy, I would like to break the code above in two lines.
x=
$(f)
Still works! But: keeping things tidy also means respecting the indentation, so that gives something like
if foo
x=
$(f)
fi
which does not work anymore due to whitespaces! Is there a good workaround for this?
bash newlines assignment
add a comment |Â
up vote
2
down vote
favorite
up vote
2
down vote
favorite
I have a function
f()
echo 777
and a variable to which I assign the "return value" of the function.
x=$(f)
Very concise! However, in my real code, the variable and function names are quite a bit longer and the function also eats positional arguments, so that concise line above, gets very long. Since I like to keep things tidy, I would like to break the code above in two lines.
x=
$(f)
Still works! But: keeping things tidy also means respecting the indentation, so that gives something like
if foo
x=
$(f)
fi
which does not work anymore due to whitespaces! Is there a good workaround for this?
bash newlines assignment
I have a function
f()
echo 777
and a variable to which I assign the "return value" of the function.
x=$(f)
Very concise! However, in my real code, the variable and function names are quite a bit longer and the function also eats positional arguments, so that concise line above, gets very long. Since I like to keep things tidy, I would like to break the code above in two lines.
x=
$(f)
Still works! But: keeping things tidy also means respecting the indentation, so that gives something like
if foo
x=
$(f)
fi
which does not work anymore due to whitespaces! Is there a good workaround for this?
bash newlines assignment
bash newlines assignment
asked 4 hours ago


pfnuesel
2,63941838
2,63941838
add a comment |Â
add a comment |Â
3 Answers
3
active
oldest
votes
up vote
0
down vote
accepted
You can wrap it in quotes:
if foo; then
x="
$(f)"
fi
Note that the indent will show/mess things up if you basically do anything other than use it plainly unquoted, though. Alternatively, you can use a subshell, which will eat the indent, instead:
if foo; then
x=$(
)$(f)
fi
Another solution:
if foo; then
set -- $(f)
x=$*
fi
New contributor
Crestwave is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.
The second and third solutions are nice! The first one not so much. I assume this is why you got a downvote, which is completely unnecessary. Thanks for your help!
– pfnuesel
10 mins ago
add a comment |Â
up vote
1
down vote
If you are allowed to use here-docs, the following style works good. Quoting the here-doc string with a leading -
allows your code to be intended with tabs only.
Something like
if true; then
read -d '' -r x <<-EOF
$(f)
EOF
fi
But remember copy pasting the code from above doesn't work as Stack Exchange replaces tabs with spaces. You need to carefully type in the Tab character for the lines starting with the here-doc and the lines ending the here-doc. My vim
configuration has mapped the tab character to 8 spaces. If you want to make it even neater, modify the spacing rule in vim
by setting the spacing for tab to 4 spaces as :set tabstop=4
You can see how the Tab is formatted in my script, by looking into it using sed
$ sed -n l script.sh
#!/usr/bin/env bash$
$
$
f()$
echo 777$
$
$
if true; then$
tread -d '' -r x <<-PERSON$
t$(f)$
tPERSON$
fi$
$
echo $x$
Notice the t
characters in the here-doc string above. If your script looks any different than the above, you would see the whining unexpected EOF
errors.
1
Very nice! Had to read it like three times, to see wherex
is assigned.
– pfnuesel
1 hour ago
add a comment |Â
up vote
1
down vote
Why split the line at the equals sign? You can just set the arguments to the function in a separate variable:
unset args
args+='arg1 '
args+='arg2 '
args+='arg3 '
x=$(f $args)
add a comment |Â
3 Answers
3
active
oldest
votes
3 Answers
3
active
oldest
votes
active
oldest
votes
active
oldest
votes
up vote
0
down vote
accepted
You can wrap it in quotes:
if foo; then
x="
$(f)"
fi
Note that the indent will show/mess things up if you basically do anything other than use it plainly unquoted, though. Alternatively, you can use a subshell, which will eat the indent, instead:
if foo; then
x=$(
)$(f)
fi
Another solution:
if foo; then
set -- $(f)
x=$*
fi
New contributor
Crestwave is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.
The second and third solutions are nice! The first one not so much. I assume this is why you got a downvote, which is completely unnecessary. Thanks for your help!
– pfnuesel
10 mins ago
add a comment |Â
up vote
0
down vote
accepted
You can wrap it in quotes:
if foo; then
x="
$(f)"
fi
Note that the indent will show/mess things up if you basically do anything other than use it plainly unquoted, though. Alternatively, you can use a subshell, which will eat the indent, instead:
if foo; then
x=$(
)$(f)
fi
Another solution:
if foo; then
set -- $(f)
x=$*
fi
New contributor
Crestwave is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.
The second and third solutions are nice! The first one not so much. I assume this is why you got a downvote, which is completely unnecessary. Thanks for your help!
– pfnuesel
10 mins ago
add a comment |Â
up vote
0
down vote
accepted
up vote
0
down vote
accepted
You can wrap it in quotes:
if foo; then
x="
$(f)"
fi
Note that the indent will show/mess things up if you basically do anything other than use it plainly unquoted, though. Alternatively, you can use a subshell, which will eat the indent, instead:
if foo; then
x=$(
)$(f)
fi
Another solution:
if foo; then
set -- $(f)
x=$*
fi
New contributor
Crestwave is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.
You can wrap it in quotes:
if foo; then
x="
$(f)"
fi
Note that the indent will show/mess things up if you basically do anything other than use it plainly unquoted, though. Alternatively, you can use a subshell, which will eat the indent, instead:
if foo; then
x=$(
)$(f)
fi
Another solution:
if foo; then
set -- $(f)
x=$*
fi
New contributor
Crestwave is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.
edited 1 hour ago
New contributor
Crestwave is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.
answered 2 hours ago
Crestwave
262
262
New contributor
Crestwave is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.
New contributor
Crestwave is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.
Crestwave is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.
The second and third solutions are nice! The first one not so much. I assume this is why you got a downvote, which is completely unnecessary. Thanks for your help!
– pfnuesel
10 mins ago
add a comment |Â
The second and third solutions are nice! The first one not so much. I assume this is why you got a downvote, which is completely unnecessary. Thanks for your help!
– pfnuesel
10 mins ago
The second and third solutions are nice! The first one not so much. I assume this is why you got a downvote, which is completely unnecessary. Thanks for your help!
– pfnuesel
10 mins ago
The second and third solutions are nice! The first one not so much. I assume this is why you got a downvote, which is completely unnecessary. Thanks for your help!
– pfnuesel
10 mins ago
add a comment |Â
up vote
1
down vote
If you are allowed to use here-docs, the following style works good. Quoting the here-doc string with a leading -
allows your code to be intended with tabs only.
Something like
if true; then
read -d '' -r x <<-EOF
$(f)
EOF
fi
But remember copy pasting the code from above doesn't work as Stack Exchange replaces tabs with spaces. You need to carefully type in the Tab character for the lines starting with the here-doc and the lines ending the here-doc. My vim
configuration has mapped the tab character to 8 spaces. If you want to make it even neater, modify the spacing rule in vim
by setting the spacing for tab to 4 spaces as :set tabstop=4
You can see how the Tab is formatted in my script, by looking into it using sed
$ sed -n l script.sh
#!/usr/bin/env bash$
$
$
f()$
echo 777$
$
$
if true; then$
tread -d '' -r x <<-PERSON$
t$(f)$
tPERSON$
fi$
$
echo $x$
Notice the t
characters in the here-doc string above. If your script looks any different than the above, you would see the whining unexpected EOF
errors.
1
Very nice! Had to read it like three times, to see wherex
is assigned.
– pfnuesel
1 hour ago
add a comment |Â
up vote
1
down vote
If you are allowed to use here-docs, the following style works good. Quoting the here-doc string with a leading -
allows your code to be intended with tabs only.
Something like
if true; then
read -d '' -r x <<-EOF
$(f)
EOF
fi
But remember copy pasting the code from above doesn't work as Stack Exchange replaces tabs with spaces. You need to carefully type in the Tab character for the lines starting with the here-doc and the lines ending the here-doc. My vim
configuration has mapped the tab character to 8 spaces. If you want to make it even neater, modify the spacing rule in vim
by setting the spacing for tab to 4 spaces as :set tabstop=4
You can see how the Tab is formatted in my script, by looking into it using sed
$ sed -n l script.sh
#!/usr/bin/env bash$
$
$
f()$
echo 777$
$
$
if true; then$
tread -d '' -r x <<-PERSON$
t$(f)$
tPERSON$
fi$
$
echo $x$
Notice the t
characters in the here-doc string above. If your script looks any different than the above, you would see the whining unexpected EOF
errors.
1
Very nice! Had to read it like three times, to see wherex
is assigned.
– pfnuesel
1 hour ago
add a comment |Â
up vote
1
down vote
up vote
1
down vote
If you are allowed to use here-docs, the following style works good. Quoting the here-doc string with a leading -
allows your code to be intended with tabs only.
Something like
if true; then
read -d '' -r x <<-EOF
$(f)
EOF
fi
But remember copy pasting the code from above doesn't work as Stack Exchange replaces tabs with spaces. You need to carefully type in the Tab character for the lines starting with the here-doc and the lines ending the here-doc. My vim
configuration has mapped the tab character to 8 spaces. If you want to make it even neater, modify the spacing rule in vim
by setting the spacing for tab to 4 spaces as :set tabstop=4
You can see how the Tab is formatted in my script, by looking into it using sed
$ sed -n l script.sh
#!/usr/bin/env bash$
$
$
f()$
echo 777$
$
$
if true; then$
tread -d '' -r x <<-PERSON$
t$(f)$
tPERSON$
fi$
$
echo $x$
Notice the t
characters in the here-doc string above. If your script looks any different than the above, you would see the whining unexpected EOF
errors.
If you are allowed to use here-docs, the following style works good. Quoting the here-doc string with a leading -
allows your code to be intended with tabs only.
Something like
if true; then
read -d '' -r x <<-EOF
$(f)
EOF
fi
But remember copy pasting the code from above doesn't work as Stack Exchange replaces tabs with spaces. You need to carefully type in the Tab character for the lines starting with the here-doc and the lines ending the here-doc. My vim
configuration has mapped the tab character to 8 spaces. If you want to make it even neater, modify the spacing rule in vim
by setting the spacing for tab to 4 spaces as :set tabstop=4
You can see how the Tab is formatted in my script, by looking into it using sed
$ sed -n l script.sh
#!/usr/bin/env bash$
$
$
f()$
echo 777$
$
$
if true; then$
tread -d '' -r x <<-PERSON$
t$(f)$
tPERSON$
fi$
$
echo $x$
Notice the t
characters in the here-doc string above. If your script looks any different than the above, you would see the whining unexpected EOF
errors.
edited 2 hours ago
answered 3 hours ago


Inian
3,053822
3,053822
1
Very nice! Had to read it like three times, to see wherex
is assigned.
– pfnuesel
1 hour ago
add a comment |Â
1
Very nice! Had to read it like three times, to see wherex
is assigned.
– pfnuesel
1 hour ago
1
1
Very nice! Had to read it like three times, to see where
x
is assigned.– pfnuesel
1 hour ago
Very nice! Had to read it like three times, to see where
x
is assigned.– pfnuesel
1 hour ago
add a comment |Â
up vote
1
down vote
Why split the line at the equals sign? You can just set the arguments to the function in a separate variable:
unset args
args+='arg1 '
args+='arg2 '
args+='arg3 '
x=$(f $args)
add a comment |Â
up vote
1
down vote
Why split the line at the equals sign? You can just set the arguments to the function in a separate variable:
unset args
args+='arg1 '
args+='arg2 '
args+='arg3 '
x=$(f $args)
add a comment |Â
up vote
1
down vote
up vote
1
down vote
Why split the line at the equals sign? You can just set the arguments to the function in a separate variable:
unset args
args+='arg1 '
args+='arg2 '
args+='arg3 '
x=$(f $args)
Why split the line at the equals sign? You can just set the arguments to the function in a separate variable:
unset args
args+='arg1 '
args+='arg2 '
args+='arg3 '
x=$(f $args)
answered 2 hours ago
ewatt
31516
31516
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%2f478804%2fassign-variable-using-multiple-lines%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