Skip to main content
We’ve updated our Terms of Service. A new AI Addendum clarifies how Stack Overflow utilizes AI interactions.
added 1 character in body; added 59 characters in body; added 11 characters in body; added 86 characters in body
Source Link
user136881
user136881

I have a small test.sh script as follows:

#!/bin/bash
read -e -p "Enter a Directory: " directory
echo $directory

Here's what happens when I run it:

$ ./test.sh 
Enter a Directory: ~/AA\ Directory/
/Users/<username>/A Directory/

I want to be able to keep the escaped space inside that variable so that the program output would read:

$ ./test.sh 
Enter a Directory: ~/AA\ Directory/
/Users/<username>/A\ Directory/

Anyone know how to do this? The use for this is to tell the script where to look for files that it needs to read.

Edit: I forgot to put the \ in the second and third code selection.

I have a small test.sh script as follows:

#!/bin/bash
read -e -p "Enter a Directory: " directory
echo $directory

Here's what happens when I run it:

$ ./test.sh 
Enter a Directory: ~/A Directory/
/Users/<username>/A Directory

I want to be able to keep the escaped space inside that variable so that the program output would read:

$ ./test.sh 
Enter a Directory: ~/A Directory/
/Users/<username>/A\ Directory

Anyone know how to do this?

I have a small test.sh script as follows:

#!/bin/bash
read -e -p "Enter a Directory: " directory
echo $directory

Here's what happens when I run it:

$ ./test.sh 
Enter a Directory: ~/A\ Directory/
/Users/<username>/A Directory/

I want to be able to keep the escaped space inside that variable so that the program output would read:

$ ./test.sh 
Enter a Directory: ~/A\ Directory/
/Users/<username>/A\ Directory/

Anyone know how to do this? The use for this is to tell the script where to look for files that it needs to read.

Edit: I forgot to put the \ in the second and third code selection.

Source Link
user136881
user136881

How to pass directory with escaped space to variable?

I have a small test.sh script as follows:

#!/bin/bash
read -e -p "Enter a Directory: " directory
echo $directory

Here's what happens when I run it:

$ ./test.sh 
Enter a Directory: ~/A Directory/
/Users/<username>/A Directory

I want to be able to keep the escaped space inside that variable so that the program output would read:

$ ./test.sh 
Enter a Directory: ~/A Directory/
/Users/<username>/A\ Directory

Anyone know how to do this?