-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!

8

1 Answer 1

-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.

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.