To be able to use a variable with $ in its value as current user as well as some other user through the su command
Clash Royale CLAN TAG#URR8PPP
up vote
2
down vote
favorite
Suppose a variable value has $
symbol (storing path of a java inner class).
I want to process it as current user as well as some other user (assume current user to be root
so that I need not enter password when using the su
command).
Example:
path_value=/home/username/filename$1.class
echo $path_value
su username -c "echo $path_value"
The result of first echo
:
/home/username/filename$1.class
The results of the second echo
inside of su
command:
/home/username/filename.class
I want to use the variable such that I would be able to process it at both places with the same value.
shell-script variable
add a comment |Â
up vote
2
down vote
favorite
Suppose a variable value has $
symbol (storing path of a java inner class).
I want to process it as current user as well as some other user (assume current user to be root
so that I need not enter password when using the su
command).
Example:
path_value=/home/username/filename$1.class
echo $path_value
su username -c "echo $path_value"
The result of first echo
:
/home/username/filename$1.class
The results of the second echo
inside of su
command:
/home/username/filename.class
I want to use the variable such that I would be able to process it at both places with the same value.
shell-script variable
add a comment |Â
up vote
2
down vote
favorite
up vote
2
down vote
favorite
Suppose a variable value has $
symbol (storing path of a java inner class).
I want to process it as current user as well as some other user (assume current user to be root
so that I need not enter password when using the su
command).
Example:
path_value=/home/username/filename$1.class
echo $path_value
su username -c "echo $path_value"
The result of first echo
:
/home/username/filename$1.class
The results of the second echo
inside of su
command:
/home/username/filename.class
I want to use the variable such that I would be able to process it at both places with the same value.
shell-script variable
Suppose a variable value has $
symbol (storing path of a java inner class).
I want to process it as current user as well as some other user (assume current user to be root
so that I need not enter password when using the su
command).
Example:
path_value=/home/username/filename$1.class
echo $path_value
su username -c "echo $path_value"
The result of first echo
:
/home/username/filename$1.class
The results of the second echo
inside of su
command:
/home/username/filename.class
I want to use the variable such that I would be able to process it at both places with the same value.
shell-script variable
edited Jun 27 at 4:54
Vlastimil
6,2511146115
6,2511146115
asked Jun 27 at 4:32
sureshcskkumar
111
111
add a comment |Â
add a comment |Â
4 Answers
4
active
oldest
votes
up vote
3
down vote
Export the value through the environment, then have the inner shell expand it. Assuming username
's login shell is Bourne-likeù:
$ÃÂ export path_value=/home/username/filename$1.class
$ÃÂ su username -c 'echo "$path_value"'
/home/username/filename$1.class
This will work even if the variable contains quotes but will not work if the command used clears the environment. I don't think su
should do that, but sudo
might.
If we know that the variable can't contain single quotesò, then just single-quoting the expanded string in the inner shell would do:
$ path_value=/home/username/filename$1.class
$ su username -c "echo '$path_value'"
/home/username/filename$1.class
(Note that the quotes are in the opposite order.)
ù if username
's login shell is csh
or tcsh
, replace echo "$path_value"
with echo $path_value:q
; if it's rc
or derivative, with echo $path_value
.
ò and newline characters if the user's login shell is csh
or tcsh
, and that it doesn't contain bytes not forming valid characters if the user's login shell is yash
. Also beware that arguments starting with -
or containing backslashes would be a problem with echo
, it's better to use printf '%sn'
for arbitrary data
2
With somesu
implementations and assuming the login shell of the user is Bourne-like, one can also do:su username -- -c 'printf "%sn" "$1"' sh "$path_value"
â Stéphane Chazelas
Jun 27 at 7:47
add a comment |Â
up vote
1
down vote
Why not use sudo
instead of su
?
# path_value=/home/username/filename$1.class
# echo "$path_value"
/home/username/filename$1.class
# sudo -u username echo "$path_value"
/home/username/filename$1.class
add a comment |Â
up vote
0
down vote
in the second echo put your variable in single quotes - like:
su username -c "echo '$path_value'"
That way echo interprets the content of the variable as a string and the "$"-sign is escaped.
add a comment |Â
up vote
0
down vote
Remember to properly quote all expansions. The shell will not peek inside single quoted strings.
path_value='/home/username/filename$1.class'
printf '%sn' "$path_value"
Outputs /home/username/filename$1.class
.
su username -c "printf '%sn' '$path_value'"
Identical output.
In the second command, the current shell will expand the value of path_value
inside tho command, as it is double quoted. The inner printf
will then print the single quoted string.
Related:
- Security implications of forgetting to quote a variable in bash/POSIX shells
- When is double-quoting necessary?
- Why is printf better than echo?
Note that $variable
is exactly the same as $variable
. Adding ...
does not quote it in any way and is only needed if the character immediately following the expansion would otherwise be taken as part of its name, as in "$variablex"
.
add a comment |Â
4 Answers
4
active
oldest
votes
4 Answers
4
active
oldest
votes
active
oldest
votes
active
oldest
votes
up vote
3
down vote
Export the value through the environment, then have the inner shell expand it. Assuming username
's login shell is Bourne-likeù:
$ÃÂ export path_value=/home/username/filename$1.class
$ÃÂ su username -c 'echo "$path_value"'
/home/username/filename$1.class
This will work even if the variable contains quotes but will not work if the command used clears the environment. I don't think su
should do that, but sudo
might.
If we know that the variable can't contain single quotesò, then just single-quoting the expanded string in the inner shell would do:
$ path_value=/home/username/filename$1.class
$ su username -c "echo '$path_value'"
/home/username/filename$1.class
(Note that the quotes are in the opposite order.)
ù if username
's login shell is csh
or tcsh
, replace echo "$path_value"
with echo $path_value:q
; if it's rc
or derivative, with echo $path_value
.
ò and newline characters if the user's login shell is csh
or tcsh
, and that it doesn't contain bytes not forming valid characters if the user's login shell is yash
. Also beware that arguments starting with -
or containing backslashes would be a problem with echo
, it's better to use printf '%sn'
for arbitrary data
2
With somesu
implementations and assuming the login shell of the user is Bourne-like, one can also do:su username -- -c 'printf "%sn" "$1"' sh "$path_value"
â Stéphane Chazelas
Jun 27 at 7:47
add a comment |Â
up vote
3
down vote
Export the value through the environment, then have the inner shell expand it. Assuming username
's login shell is Bourne-likeù:
$ÃÂ export path_value=/home/username/filename$1.class
$ÃÂ su username -c 'echo "$path_value"'
/home/username/filename$1.class
This will work even if the variable contains quotes but will not work if the command used clears the environment. I don't think su
should do that, but sudo
might.
If we know that the variable can't contain single quotesò, then just single-quoting the expanded string in the inner shell would do:
$ path_value=/home/username/filename$1.class
$ su username -c "echo '$path_value'"
/home/username/filename$1.class
(Note that the quotes are in the opposite order.)
ù if username
's login shell is csh
or tcsh
, replace echo "$path_value"
with echo $path_value:q
; if it's rc
or derivative, with echo $path_value
.
ò and newline characters if the user's login shell is csh
or tcsh
, and that it doesn't contain bytes not forming valid characters if the user's login shell is yash
. Also beware that arguments starting with -
or containing backslashes would be a problem with echo
, it's better to use printf '%sn'
for arbitrary data
2
With somesu
implementations and assuming the login shell of the user is Bourne-like, one can also do:su username -- -c 'printf "%sn" "$1"' sh "$path_value"
â Stéphane Chazelas
Jun 27 at 7:47
add a comment |Â
up vote
3
down vote
up vote
3
down vote
Export the value through the environment, then have the inner shell expand it. Assuming username
's login shell is Bourne-likeù:
$ÃÂ export path_value=/home/username/filename$1.class
$ÃÂ su username -c 'echo "$path_value"'
/home/username/filename$1.class
This will work even if the variable contains quotes but will not work if the command used clears the environment. I don't think su
should do that, but sudo
might.
If we know that the variable can't contain single quotesò, then just single-quoting the expanded string in the inner shell would do:
$ path_value=/home/username/filename$1.class
$ su username -c "echo '$path_value'"
/home/username/filename$1.class
(Note that the quotes are in the opposite order.)
ù if username
's login shell is csh
or tcsh
, replace echo "$path_value"
with echo $path_value:q
; if it's rc
or derivative, with echo $path_value
.
ò and newline characters if the user's login shell is csh
or tcsh
, and that it doesn't contain bytes not forming valid characters if the user's login shell is yash
. Also beware that arguments starting with -
or containing backslashes would be a problem with echo
, it's better to use printf '%sn'
for arbitrary data
Export the value through the environment, then have the inner shell expand it. Assuming username
's login shell is Bourne-likeù:
$ÃÂ export path_value=/home/username/filename$1.class
$ÃÂ su username -c 'echo "$path_value"'
/home/username/filename$1.class
This will work even if the variable contains quotes but will not work if the command used clears the environment. I don't think su
should do that, but sudo
might.
If we know that the variable can't contain single quotesò, then just single-quoting the expanded string in the inner shell would do:
$ path_value=/home/username/filename$1.class
$ su username -c "echo '$path_value'"
/home/username/filename$1.class
(Note that the quotes are in the opposite order.)
ù if username
's login shell is csh
or tcsh
, replace echo "$path_value"
with echo $path_value:q
; if it's rc
or derivative, with echo $path_value
.
ò and newline characters if the user's login shell is csh
or tcsh
, and that it doesn't contain bytes not forming valid characters if the user's login shell is yash
. Also beware that arguments starting with -
or containing backslashes would be a problem with echo
, it's better to use printf '%sn'
for arbitrary data
edited Jun 27 at 9:22
Stéphane Chazelas
278k52513844
278k52513844
answered Jun 27 at 7:20
ilkkachu
47.3k668130
47.3k668130
2
With somesu
implementations and assuming the login shell of the user is Bourne-like, one can also do:su username -- -c 'printf "%sn" "$1"' sh "$path_value"
â Stéphane Chazelas
Jun 27 at 7:47
add a comment |Â
2
With somesu
implementations and assuming the login shell of the user is Bourne-like, one can also do:su username -- -c 'printf "%sn" "$1"' sh "$path_value"
â Stéphane Chazelas
Jun 27 at 7:47
2
2
With some
su
implementations and assuming the login shell of the user is Bourne-like, one can also do: su username -- -c 'printf "%sn" "$1"' sh "$path_value"
â Stéphane Chazelas
Jun 27 at 7:47
With some
su
implementations and assuming the login shell of the user is Bourne-like, one can also do: su username -- -c 'printf "%sn" "$1"' sh "$path_value"
â Stéphane Chazelas
Jun 27 at 7:47
add a comment |Â
up vote
1
down vote
Why not use sudo
instead of su
?
# path_value=/home/username/filename$1.class
# echo "$path_value"
/home/username/filename$1.class
# sudo -u username echo "$path_value"
/home/username/filename$1.class
add a comment |Â
up vote
1
down vote
Why not use sudo
instead of su
?
# path_value=/home/username/filename$1.class
# echo "$path_value"
/home/username/filename$1.class
# sudo -u username echo "$path_value"
/home/username/filename$1.class
add a comment |Â
up vote
1
down vote
up vote
1
down vote
Why not use sudo
instead of su
?
# path_value=/home/username/filename$1.class
# echo "$path_value"
/home/username/filename$1.class
# sudo -u username echo "$path_value"
/home/username/filename$1.class
Why not use sudo
instead of su
?
# path_value=/home/username/filename$1.class
# echo "$path_value"
/home/username/filename$1.class
# sudo -u username echo "$path_value"
/home/username/filename$1.class
edited Jun 27 at 9:24
Stéphane Chazelas
278k52513844
278k52513844
answered Jun 27 at 5:03
Deathgrip
1,266311
1,266311
add a comment |Â
add a comment |Â
up vote
0
down vote
in the second echo put your variable in single quotes - like:
su username -c "echo '$path_value'"
That way echo interprets the content of the variable as a string and the "$"-sign is escaped.
add a comment |Â
up vote
0
down vote
in the second echo put your variable in single quotes - like:
su username -c "echo '$path_value'"
That way echo interprets the content of the variable as a string and the "$"-sign is escaped.
add a comment |Â
up vote
0
down vote
up vote
0
down vote
in the second echo put your variable in single quotes - like:
su username -c "echo '$path_value'"
That way echo interprets the content of the variable as a string and the "$"-sign is escaped.
in the second echo put your variable in single quotes - like:
su username -c "echo '$path_value'"
That way echo interprets the content of the variable as a string and the "$"-sign is escaped.
answered Jun 27 at 4:59
rohr
512
512
add a comment |Â
add a comment |Â
up vote
0
down vote
Remember to properly quote all expansions. The shell will not peek inside single quoted strings.
path_value='/home/username/filename$1.class'
printf '%sn' "$path_value"
Outputs /home/username/filename$1.class
.
su username -c "printf '%sn' '$path_value'"
Identical output.
In the second command, the current shell will expand the value of path_value
inside tho command, as it is double quoted. The inner printf
will then print the single quoted string.
Related:
- Security implications of forgetting to quote a variable in bash/POSIX shells
- When is double-quoting necessary?
- Why is printf better than echo?
Note that $variable
is exactly the same as $variable
. Adding ...
does not quote it in any way and is only needed if the character immediately following the expansion would otherwise be taken as part of its name, as in "$variablex"
.
add a comment |Â
up vote
0
down vote
Remember to properly quote all expansions. The shell will not peek inside single quoted strings.
path_value='/home/username/filename$1.class'
printf '%sn' "$path_value"
Outputs /home/username/filename$1.class
.
su username -c "printf '%sn' '$path_value'"
Identical output.
In the second command, the current shell will expand the value of path_value
inside tho command, as it is double quoted. The inner printf
will then print the single quoted string.
Related:
- Security implications of forgetting to quote a variable in bash/POSIX shells
- When is double-quoting necessary?
- Why is printf better than echo?
Note that $variable
is exactly the same as $variable
. Adding ...
does not quote it in any way and is only needed if the character immediately following the expansion would otherwise be taken as part of its name, as in "$variablex"
.
add a comment |Â
up vote
0
down vote
up vote
0
down vote
Remember to properly quote all expansions. The shell will not peek inside single quoted strings.
path_value='/home/username/filename$1.class'
printf '%sn' "$path_value"
Outputs /home/username/filename$1.class
.
su username -c "printf '%sn' '$path_value'"
Identical output.
In the second command, the current shell will expand the value of path_value
inside tho command, as it is double quoted. The inner printf
will then print the single quoted string.
Related:
- Security implications of forgetting to quote a variable in bash/POSIX shells
- When is double-quoting necessary?
- Why is printf better than echo?
Note that $variable
is exactly the same as $variable
. Adding ...
does not quote it in any way and is only needed if the character immediately following the expansion would otherwise be taken as part of its name, as in "$variablex"
.
Remember to properly quote all expansions. The shell will not peek inside single quoted strings.
path_value='/home/username/filename$1.class'
printf '%sn' "$path_value"
Outputs /home/username/filename$1.class
.
su username -c "printf '%sn' '$path_value'"
Identical output.
In the second command, the current shell will expand the value of path_value
inside tho command, as it is double quoted. The inner printf
will then print the single quoted string.
Related:
- Security implications of forgetting to quote a variable in bash/POSIX shells
- When is double-quoting necessary?
- Why is printf better than echo?
Note that $variable
is exactly the same as $variable
. Adding ...
does not quote it in any way and is only needed if the character immediately following the expansion would otherwise be taken as part of its name, as in "$variablex"
.
edited Jun 27 at 7:10
answered Jun 27 at 7:01
Kusalananda
101k13199312
101k13199312
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%2f452134%2fto-be-able-to-use-a-variable-with-in-its-value-as-current-user-as-well-as-some%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