ProgrammingThis forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.
Notices
Welcome to LinuxQuestions.org, a friendly and active Linux Community.
You are currently viewing LQ as a guest. By joining our community you will have the ability to post topics, receive our newsletter, use the advanced search, subscribe to threads and access many other special features. Registration is quick, simple and absolutely free. Join our community today!
Note that registered members see fewer ads, and ContentLink is completely disabled once you log in.
If you have any problems with the registration process or your account login, please contact us. If you need to reset your password, click here.
Having a problem logging in? Please visit this page to clear all LQ-related cookies.
Get a virtual cloud desktop with the Linux distro that you want in less than five minutes with Shells! With over 10 pre-installed distros to choose from, the worry-free installation life is here! Whether you are a digital nomad or just looking for flexibility, Shells can put your Linux machine on the device that you want to use.
Exclusive for LQ members, get up to 45% off per month. Click here for more info.
So I recently hit this issue at work and have discovered it is to do with the version of bash being run.
That being said, I need to find an alternative to get this option to work again.
The scenario is, I need to be able to source a file into my environment and then utilise the functions/variables that are from the sourced file.
So in bash v4.1:
Code:
$ sudo -u user cat /home/user/file
x=hello
$ sudo -u user -i bash -c '. file && set | grep hello && echo "#$x#"'
BASH_EXECUTION_STRING='. f1 && set | grep hello && echo "#$x#"'
SUDO_COMMAND='/bin/bash -c bash -c .\ f1\ &&\ set\ |\ grep\ hello\ &&\ echo\ "#$x#"'
x=hello
#hello#
Current version at work is 4.2+ and at home is 5.1.16, and they both produce this:
Code:
$ sudo -u user cat /home/user/file
x=hello
$ sudo -u user -i bash -c '. file && set | grep hello && echo "#$x#"'
BASH_EXECUTION_STRING='. f1 && set | grep hello && echo "##"'
SUDO_COMMAND='/bin/bash -c bash -c .\ f1\ &&\ set\ |\ grep\ hello\ &&\ echo\ "#$x#"'
x=hello
##
Now there are two things to note here:
1. The line after x=hello which is my echo'ed output (which should be #hello#)
2. If you look at the BASH_EXECUTION_STRING it has already expanded the $x to be nothing?? My understanding here is as the commands
being run are in single quotes the variable should not get expanded until after the login and see the exported variable
My understanding is this is probably some kind of security feature, however, is it possible at all to source a file once logged in
via sudo as a user and then recall the source'd material?
Please let me know if any additional information is required?
Not that I expected it to, but happy to test any option
As this is how we run many different commands on our system and with varying uses of the source'd material, it is less than practical to have a script for all of them
Also, the general feedback is, "Why did it just stop working?"
As for why it changed, if you've identified the version when it changed, you should be able to consult the relevant section of the bash changelog, then identify the relevant issue / commit message to find out the reason.
So it was working. If I remember well there were some incompatible changes around bash 4.2, but I can't recall it (which was probably related to it).
You might want to escape that $ sign anyway.
@pan64 --- yep tried with escaping, still no change
@boughtonp --- whilst you are correct that shifting some into .bash_profile can fix the issue, we also have a situation where the same variable gets over-written by source'ing a different
file, so this would only fix some of our issues and not the main issue as a whole
I will see if I can find the correct changelog, however, not sure this will change the outcome?
I futzed around for a bit, and found that ${x} seems to work, even though $x doesn't!? Also, I suspect this might be a difference in sudo rather than bash (I don't have an older bash handy, but the difference seems to show up with just printf too):
Code:
$ sudo -u $USER -i bash -c '. f1 && set | grep hello && echo "#$x#${x}#"'
BASH_EXECUTION_STRING='. f1 && set | grep hello && echo "##${x}#"'
SUDO_COMMAND='/bin/bash -c bash -c .\ f1\ &&\ set\ |\ grep\ hello\ &&\ echo\ "#$x#${x}#"'
x=hello
##hello#
$ sudo -u $USER -i /usr/bin/printf '[%s]\n' '${x}' aa '#$x#'
[${x}]
[aa]
[##]
$ bash --version
GNU bash, version 5.0.3(1)-release (x86_64-pc-linux-gnu)
Copyright (C) 2019 Free Software Foundation, Inc.
License GPLv3+: GNU GPL version 3 or later <http://gnu.org/licenses/gpl.html>
This is free software; you are free to change and redistribute it.
There is NO WARRANTY, to the extent permitted by law.
vpsuser720@vpsuser-720:~$ sudo --version
Sudo version 1.8.27
Sudoers policy plugin version 1.8.27
Sudoers file grammar version 46
Sudoers I/O plugin version 1.8.27
Well I know some of my team mates will be less than happy as they already dread sudo with a passion, but adding curly braces definitely saves
me a lot of time
Thanks again ... as usual, still love this community after all this time
I futzed around for a bit, and found that ${x} seems to work, even though $x doesn't!? Also, I suspect this might be a difference in sudo rather than bash (I don't have an older bash handy, but the difference seems to show up with just printf too):
Very good. From my side I think it is related to bash, how it evaluates this command line and that had been "slightly" changed between 4.1 and 4.3. Unfortunately I can't find the relevant info and also I can't test it, but anyway you found it.
@NevemTeve --- The -i is a requirement as the user's environment also sets many other options which are also required, in addition to the source'd material. Also, as advised initially, this all works with lower version of bash / sudo
LinuxQuestions.org is looking for people interested in writing
Editorials, Articles, Reviews, and more. If you'd like to contribute
content, let us know.