when file name has space, how to use variable in script? [duplicate] - bash

This question already has answers here:
When to wrap quotes around a shell variable?
(5 answers)
Closed 10 months ago.
I want to open a application using shell script.
But! when file name has space, script read this as two files.
1 path=/Applications
2 program=/Google Chrome.app
3 open $path$program
it's not working...
when I use alias like these...
program="/Google Chrome.app" or
program=/Google\ Chrome.app or
program="/Google\ Chrome.app"
shell cognized two files like this
"The files /Applications/Google and /current_dir/Chrome.app do not exist."
How I fix that?
please give me your teaching 🧐

You can wrap your variables in quotes. So what you can do is:
path=Applications
program="Google Chrome.app"
open /$path/$program

Related

How to use whitespace in directories with a Bash script? [duplicate]

This question already has answers here:
How do I pass on script arguments that contain quotes/spaces?
(2 answers)
Bash script to cd to directory with spaces in pathname
(14 answers)
Closed 24 days ago.
I have a script that appends the date and time to all files in a folder. I use the script like this...
bash append_date.sh /home/user/Documents/Podcasts/
and that will append the date to all files in the /home/user/Documents/Podcasts/ folder
Problem is that if there is a whitespace in the directory tree it fails to do anything. ie
bash append_date.sh /home/user/Documents/My Stuff/
I have tried passing the following, but that does not work
bash append_date.sh /home/user/Documents/My\ Stuff/
How do I get this script to play nice with whitespaces?
Many thanks for any help.

Wierd bash behaiviour with vim [duplicate]

This question already has answers here:
When to wrap quotes around a shell variable?
(5 answers)
Why does shell ignore quoting characters in arguments passed to it through variables? [duplicate]
(3 answers)
Closed 7 months ago.
To recreate this you will need to create a file named "l l.tex" in your home directory. Then make a an executable script with the following code
alacritty -e vim l\ l.tex
This opens the file, as I desired. Now create the following script
var="/home/l\ l.tex"
dar=$(echo $var | sed 's/\/home\//\#/g')
bar=$(echo $dar | sed 's/#//g')
alacritty -e vim $bar
This script, however, makes two new documents. However, they should be the same. I think there is an issue with how $bar is read. I am novice at this, but I reckon that this has to do with how strings are stored.
Can someone help me fix the second code so that I can use a variable?

Find out what the (shell) script was invoked with [duplicate]

This question already has answers here:
How do I parse command line arguments in Bash?
(40 answers)
How to get exact command line string from shell?
(2 answers)
Closed 3 years ago.
Suppose my script.sh could take a number of options and arguments. What is the best way to find out what the script was invoked with (form inside the script)?
For eg., someone called it with script.sh --foo_option bar_arg
Is there a way to echo that exact command they typed from inside the script?
I've tried echo !! which does not work inside a script.

How can I access a directory that is in $HOME using bash? [duplicate]

This question already has answers here:
"~/Desktop/test.txt: No such file or directory"
(2 answers)
Why isn't tilde (~) expanding inside double quotes? [duplicate]
(1 answer)
Closed 4 years ago.
I'm new to Linux as well as bash. I made a script that accesses a folder that is located in the home directory, but the script will not always be called from the home directory. The prompt I'm getting when calling it from any subdirectories specifies that it can not find the file.
Here's an example of what I'm trying to do:
for entry in "~/.directory"/*
do
echo "$entry"
done
If I place the script in a subdirectory of /home and try to call it, the script is unable to find the directory. I know it exists as if I run ls ~/.directory in the subdirectory it is able to find the files and print them with no problem. Is there a different way I should be trying to access the directory in the bash shell? Thanks!
Voted to close my question. It seems rather specific to me, and the general solution was something I found earlier and was also posted in the comments below. I'll figure it out eventually -
Only unquoted tildes are expanded.
for entry in ~/".directory"/*

What is {} used for in this context? [duplicate]

This question already has answers here:
When do we need curly braces around shell variables?
(7 answers)
Closed 6 years ago.
It is doing something to my path_bash variable but what?
Google pulls up this but I can't find it exactly.
#!/bin/bash
path_bash="$HOME/root/config/bash/"
source "${path_bash}_private.sh"
source "${path_bash}config.sh"
source "${path_bash}utility.sh"
source "${path_bash}workflow.sh"
source "${path_bash}net.sh"
source "${path_bash}makeHTM.sh"
and can I put
path_bash
in another file?
It's used to tell bash where the name of your variable ends.
And example to explain:
$ a="gg"
$ echo $ab
$ echo ${a}b
ggb

Resources