How to cd to path in a variable in bash script [duplicate] - bash

This question already has an answer here:
Why is a tilde in a path not expanded in a shell script?
(1 answer)
Closed 10 months ago.
Im trying to do cd to a path that is in variable called path. But cannot cd into it.
$repo="test"
path="~/code/forks/$repo/client"
echo $path #echoing correct working path
cd "$path"
pwd

The quotes are preventing the shell from expanding ~ to your home directory. (~ is a shell feature, not an actual directory name.)
path=~/code/forks/$repo/client
This expands ~ during the assignment, not when the parameter is expanded.

Related

Cut off the path of the working directory in the Linux console [duplicate]

This question already has answers here:
How can I shortern my command line prompt's current directory?
(12 answers)
Closed 2 years ago.
When I am getting into my working directory, I have the next pathname in the console:
kravcneger#kravcneger-X751L:~/projects/gcc/my_project
That path is very long, and it increases the width of the terminal window.
How can I make the pathname shorter, so that I wouldn't have to expand the terminal for comfortable work?
A critical condition: to change the machine name and the working directory is prohibited. :)
Add (or change) in your ~/.bashrc file PS1 variable:
PS1='\h \W\$ '
Here, \h is the machine name, \W is the basename of the current directory, and \$ is the literal $.
SEE ALSO:
Controlling the Prompt (Bash Reference Manual)

Aliased command in bashrc does not correctly use $OLDPWD [duplicate]

This question already has answers here:
Difference between single and double quotes in Bash
(7 answers)
Closed 2 years ago.
I have an alias in bashrc that I use to generate a particular LaTeX document (the main command used is pdflatex) but I want to be able to execute it regardless of where I am. Unfortunately, it seems like you can't use use this command with an absolute path unless the path is 'under' your current directory, so I cd into the directory my .tex file is, run pdflatex, then try to cd back to my previous directory. In bashrc, I have my command as
alias nbr="cd ~/path/to/dir && pdflatex file.tex && cd $OLDPWD"
The command works fine outside of putting me back in my previous directory. For some reason, this command works perfectly fine in a regular bash shell, so I'm guessing there's some issue with what $OLDPWD is considered in the context of bashrc but I'm not sure. Any ideas?
#Cyrus's answer fixed it...should've used single quotes.

Get bash where bash script executed from [duplicate]

This question already has answers here:
Shell script current directory?
(7 answers)
Closed 2 years ago.
I want to be able to execute a bash script located in my home directory with the command ~/script_name.sh from any directory and then in the script, get the directory that it was ran from.
eg. I'm in the directory /foo/bar/baz, and execute /foo/script.sh, and it prints out /foo/bar/baz
pwd and $0/$BASH_SOURCE give me the directory my shell starts in and the path to the script in my home directory respectively.
Instead of invoking the external command pwd, consider using $PWD instead. If you want to protect against some rogue code having explicitly changed PWD, do a cd . (which is an internal command) first, which restores PWD to the correct value.

Bash script check if tilde '~' is an argument [duplicate]

This question already has answers here:
Echoing a tilde to a file without expanding it in Bash
(3 answers)
Pass a special variable (~ tilde) to Java program
(1 answer)
Closed 5 years ago.
I have a simple bash script that can accept arguments that it will be treating as text strings, nothing more.
If I give it ~, without quotes, then the home directory /home/users/me is what's parsed. Quote it, "~", it's fine. The character "~" is what I want, not the home path.
Is there any way I can ensure an un-quoted ~ is treated exactly as the character "~", not the home directory alias?
The bash shell is expanding the ~ on the command line before the argument is passed to your script.
There might be a bash option that you can change in your shell, but that would affect everything in your shell, which doesn't sound like what you want.
The short answer I think is no. There's nothing you can do in your script to change how the parent shell expanded any arguments before passing them to your script.

How to replace ~ with /Users/$USER in a shell script [duplicate]

This question already has an answer here:
Bash path variable using ~ resulting in 'No such file or directory'
(1 answer)
Closed 7 years ago.
In a bash script, I want the user to enter a path to an application, and then launch that application. By default, the application is expected to be in the user's own directory. The following script works for me on Mac OS X, but it is not particularly elegant:
path_default="~/neo4j/bin/neo4j"
read -p "Enter path to neo4j [$path_default]: " path
path="${path:-$path_default}"
if [[ ${path:0:1} == "~" ]]; then
path="/Users/$USER"${path#"~"}
fi
$path start
How can I improve this so that it will work on any platform?
if you consider ~ can be in any platform you can use a variable, for example userpath=$(echo ~)
Try this:
path_default="$HOME/bin/neo4j"
read -p "Enter path to neo4j [$path_default]: " path
path="${path:-$path_default}"
path="${path/#\~/$HOME}"
Here, tilde in the beginning of the path variable is substituted by the contents of $HOME variable.

Resources