This question already has answers here:
How can I cd into a directory using a script?
(4 answers)
Closed 7 years ago.
My script
#!/bin/bash
for file in *.ats; do
mv $file /home/holmes/procmt
done
cd /home/holmes/procmt
All files are moved but I want to change my current directory to /home/holmes/procmt and nothing happens.WHY?
If i run script ./pch.sh,I stay in the same shell.
The shell running the script moved, but the shell you were typing in when you started the script stayed put. If you precede the name of your shell script with . (dot space) it will run in 'your' shell.
Related
This question already has answers here:
How to assign a glob expression to a variable in a Bash script?
(8 answers)
Wildcard within quotations
(3 answers)
Closed 8 months ago.
I am unable to copy files using Shell script. I validated in shellcheck.net no issues found in syntax.
It showing error as file not fount but I am using exact file name.
Below is my Shell script.
#! /bin/bash
export SOURCE_PATH="/home/la1122/export"
export SOURCE_TARGET="/home/la1122/export/backup"
FILE="*.dat"
cd $SOURCE_PATH
echo "Current location: $(pwd)"
cp "$FILE" "$SOURCE_TARGET"
This question already has answers here:
When to wrap quotes around a shell variable?
(5 answers)
Change the current directory from a Bash script
(17 answers)
How can I store a command in a variable in a shell script?
(12 answers)
Closed 2 years ago.
Summary:
I am trying to have a script that allows easy one-touch access to files that are generated by the crontab.
The hourly generated files go by the path as: /home/mouse/20210126/0900.
Of course as the date and time changes, the path will change like so:
/home/mouse/20210126/1000
/home/mouse/20210126/1100
/home/mouse/20210127/1000
/home/mouse/20210128/1300
I tried using an alias, but once .bashrc is loaded, it only takes the present date/time, which means that I cannot "refresh" the date and time.
So by using a script, I could update the date and time like so:
currentdate=$(date +%Y%m%d)
currenttime=$(date -d '1 hour ago' "+%H00")
collect=cd /home/mouse/$currentdate/$currenttime
echo $currentdate
echo $currenttime
$collect
However, I realized that the cd script is not working because, from my understanding, it works on a subshell and once the script has finished executing, it does not affect the main shell.
I tried source / . but I get /home/mouse/20210126/1000 is a directory.
What is the recommended action I should take to resolve this?
This question already has answers here:
what's the difference between ./script.sh and bash script.sh
(5 answers)
Difference between ./ and ~/
(6 answers)
Closed 3 years ago.
I downloaded IntelliJ IDEA for my linux system. It says to add the location of the bin folder of IntelliJ to the PATH environment variable in order to run IntelliJ from any folder.
So, I opened .bashrc and put in the address -
'export PATH=$PATH:/Documents/IntelliJ/bin' and then 'source ~/.bashrc'.
Then, to check I echoed by PATH and it showed the changes.
But, when I try to run './idea.sh' from some other directory it does not work. It says file not found. I have no idea what's hapenning?
This question already has answers here:
Why can't I change directories using "cd" in a script?
(33 answers)
Closed 6 years ago.
I am new to bash scripting. This might be obvious to many. please bear with me.
I have a shell script as follows:
#!/bin/bash
echo `pwd`
cd /home/foo/bar
echo `pwd`
Let's say I am currently in dir : /home/foo1
If I execute the above script it prints:
/home/foo1
/home/foo/bar
But once the script completes execution, I have seen that it still remains in dir /home/foo1
I have also seen some scripts where there are explicit commands to reset the working dir using 'cd -' command.
If bash executes all the lines in the script as commands, why does it again reset the working dir?
When you are running an interactive session of bash, and from it, you execute a script (e.g. ./myscript.sh), then bash creates a new bash process to execute the script. Initially, that process get copy of the same environment as the original process (e.g. the current working directory, or environment variables), but if the script modifies the environment somehow, this changes only affect the new process, not the original one. So when the scripts exits, you go back to the original process which retains the original environment. So it is not possible to modify the current directory of the original shell from a script.
As a side note, the following line
echo `pwd`
does not make much sense. You either have to do echo $PWD or simply pwd.
This question already has answers here:
How do you normalize a file path in Bash?
(24 answers)
Closed 6 years ago.
I've a shell script at
/home/abc/xyz/test.sh
I've a file at
/home/def/ghi/jkl/lmn/foo.txt
I'm running the scrip from /home as I've set my PATH variable to point to /home/abc/xyz/
I'm passing the relative path of foo.txt to my script like so test ./def/ghi/jkl/lmn/foo.txt
I want to capture the full path of foo.txt into a variable in my script, any pointers?
Tried https://stackoverflow.com/a/5265775 and https://stackoverflow.com/a/9107028/4468505 but in vain.
foobar=`readlink -f <passed in relative path to foo.txt>`