-2

I'm a beginner in Linux scripting. Somehow I can't get the right search string to find the answer.

I want to run a script relative to a specific user's directory like:

~user/rel/path/to/script.sh

but I don't know what "~user" will translate to. It may even contain spaces. Should I quote it? And if so, how? I always get the file or folder does not exist error when I try to use quotes.

Edit

This is not a duplicate I think.

My concern was that running the following with quotes:

"~user/rel/path/to/script.sh"

gives me "file or folder not found" error. But I don't know, what ~user will translate to. (The script will be called on many different computers. The username is given but the home directory may be changed freely by the owner of each computer.) So I was afraid (as a Linux scripting BEGINNER!!!) to run it without quotes like:

~user/rel/path/to/script.sh

The most down-voted answer (Java system properties and environment variables) actually helped me most. I just needed to confirm that it works the same way on Linux. So I installed a test VM in VirtualBox and tried:

cd /
sudo mkdir -p "test home dir/myuser"
sudo adduser myuser
sudo chown myuser:myuser "test home dir/myuser"
sudo usermod -d "/test home dir/myuser" myuser
su myuser
cd ~
echo '#!/bin/bash -x
  echo "here!"
' > test.sh
chmod +x test.sh 
exit
cd /
~myuser/test.sh

And it worked!

E_net4
  • 27,810
  • 13
  • 101
  • 139
tomorrow
  • 1,260
  • 1
  • 14
  • 26
  • Can you post your actual bash code you're trying to run. It will be more helpful if there was actual code/commands to look at – francium Jun 09 '18 at 22:30
  • You can do `ls ~user` to find out what `~` is. – Brandon Miller Jun 09 '18 at 22:38
  • Execute pwd in the scripts directory and use the full path – AHT Jun 09 '18 at 23:13
  • Possible duplicate of [Shell script with whitespace in path executes differently depending on directory](https://stackoverflow.com/q/3746458/608639), [Bash variables with spaces](https://stackoverflow.com/q/45219905/608639), [Spaces with bash](https://stackoverflow.com/q/11523015/608639), [How to cd into a directory with space in the name?](https://stackoverflow.com/q/18323508/), [How to add Path with space in bash variable](https://stackoverflow.com/q/43787476/), [Shell script with whitespace in path executes differently depending on directory](https://stackoverflow.com/q/3746458/), etc. – jww Jun 09 '18 at 23:30
  • [Relative paths based on file location instead of current working directory](https://stackoverflow.com/q/24112727/608639), [Getting the source directory of a Bash script from within](https://stackoverflow.com/q/59895/608639), [Reliable way for a bash script to get the full path to itself?](https://stackoverflow.com/q/4774054/608639), [Get current directory of file after getting called by another bash script](https://stackoverflow.com/q/16850029/608639), etc. – jww Jun 09 '18 at 23:37
  • Depending on your distribution -- the problem is most likely the fact that you do not have permission to descend into another's home directory. (access is controlled by the `x`, execute bit on a directory). So, `ls -al /home` and look at the permissions on the directory you want to enter and also the group to which that directory belongs. The permissions are `drwxrwxrwx`, where `d` means it is a directory, the first `rwx` is the user permissions, the second `rwx` is the group permissions and the final `rwx` is world (everybody) permissions. You must have the `x` for the group you fall in. – David C. Rankin Jun 10 '18 at 00:15
  • Further `~user` doesn't translate to anything but `~user`. When unquoted and expanded by the shell `~/` will expand to `/home/you` (where `you` will be your user name). You cannot do anything like `~bill` and expect any expansion to bill's home directory. Try it `echo ~bill` and `echo ~/` or just `echo ~` (depending on whether you want the trailing `'/'` appended). – David C. Rankin Jun 10 '18 at 00:20
  • Well, I found `~user` somewhere and it seems to work well on Xubuntu. Do you mean that it's not available on other distros? – tomorrow Jun 11 '18 at 09:27

1 Answers1

-1

On Mac OS you don't need to quote. I'm not sure about Linux. However, if

ls ~user

would result in /dir with space/user/ then

sh ~user/rel/path/to/script.sh

would be

sh /dir\ with\ space/user/rel/path/to/script.sh

and this executes if you have set the execution flag on script.sh accordingly.

stack_lech
  • 990
  • 2
  • 10
  • 28