Shell script change directory with variable
You variable contains a carriage return. Try saying:
cd $(echo $RED_INSTANCE_NAME | tr -d '\r')
and it should work. In order to remove the CR from the variable you can say:
RED_INSTANCE_NAME=$(echo $RED_INSTANCE_NAME | tr -d '\r')
The following would illustrate the issue:
$ mkdir abc
$ foo=abc$'\r'
$ echo "${foo}"
abc
$ cd "${foo}"
: No such file or directory
$ echo $foo | od -x
0000000 6261 0d63 000a
0000005
$ echo $foo | tr -d '\r' | od -x
0000000 6261 0a63
0000004
$ echo $'\r' | od -x
0000000 0a0d
0000002
how to change directory to a path stored in variable bash
Try this:
cd $(eval "echo \"$(cat $HOME/devdir.txt)\"");
or:
eval "cd \"$(cat $HOME/devdir.txt)\"";
It will change the directory inside the script
session that you are running. If you run ./script
it won't cd
externally
Bash: use cd command on a variable including spaces
Why don't you just...
cd "$input_path"
since it is quoted, there won't be any problem with spaces.
By saying cd $(echo "$input_path")
you are in fact saying cd my path
, whereas you want to do cd "my path"
. Thus, as commented by JID below, you can also say cd "$(echo $input_path)"
because the important quotes are the ones that are "closer" to cd
.
If you don't quote, cd
sees:
cd my path
So it tries to cd my
, whereas if you quote it sees:
cd "my path"
How can i change directory in shell script with variables
I would suggest a function called "prj" to put in your .bashrc
:
prj () {
cd /home/byram/workspace/"$1"/src/com/seri
}
Then use it like this
prj 1 # Switch to ...1/src/com/seri
prj 2 # Switch to ...2/src/com/seri
Bash script to change directory from user input
It is better to use quotes in the cd command, regardless of whether the directory has spaces or not, like this:
#!/bin/bash
read -p "Enter destination: " folder
cd "$folder"
pwd
Test:
Another solution (use with caution as it may cause other problems) is using eval in your code:
#!/bin/bash
read -p "Enter destination: " folder
folder=$(sed -e 's/^/"/' -e 's/$/"/' <<< $folder)
eval cd $folder
References:
- Bash script to cd to directory with spaces in pathname
Cannot cd with external variable in bash script
The problem came from invsible \r
(carriage return) generated by windows at the end of lines.
I change the EOL Conversion to Unix (LF)
!
How to use variable in bash script
Spacing is important. Because of the space in your command, this doesn't do what you expect:
user= whoami
What that does is define an empty variable called user
, and then calls the whoami
command with that empty variable added to the environment. The "pbsh56" you see is from the whoami
command, not from the echo
.
What you want is
user=$(whoami)
echo $user
cd /home/$user
Change directory in script bash
Source your script instead of running it like you do. When you run it like you do, you spawn a new shell that executes the cd
, completes succesfully, closes the shell and returns to your current shell, leaving you in your pwd
.
Use source myscript.sh
or . myscript.sh
instead of bash myscript.sh
or myscript.sh
.
Related Topics
Option to Display Control Characters in Gedit
Magento Apache Configuration (App/Etc/Local.Xml Accessible Warning)
Which Stack Is Used by Interrupt Handler - Linux
Poorly-Balanced Socket Accepts with Linux 3.2 Kernel VS 2.6 Kernel
How to Release Hugepages from the Crashed Application
Add Some Specific Time While Using the Linux Command "Date"
What's the Difference B/W _Raw_Readl/_Raw_Writel and Readl/Writel in Linux Kernel
Linux Assembly: How to Call Syscall
How to Write Full-Screen Linux Console App/Script
How to Tie a Network Connection to a Pid Without Using Lsof or Netstat
"Cd" Does Not Work in Shell Script
How to Prefill Command Line Input
How to Run Linux Docker Images on Windows Server 2016
Git- How to Kill Ssh-Agent Properly on Linux
How to Implement a Practical Fiber Scheduler