1

I want to get datetime and make a directory named as such, then cd to this directory.

This is my bash script:

t=$(date +%F-%H%M%S) 
mkdir $t
cd $t

but it doesn't work fully.

It creates a directory, but the cd command doesn't work.

Screenshot:

screenshot of command prompt

Benjamin W.
  • 46,058
  • 19
  • 106
  • 116
KBKai
  • 353
  • 1
  • 4
  • 18
  • 1
    What does it mean "it's not working"? It perfectly works. If you want any help then describe how it actually behaves. "It's not working" is a nonsense. – David Ferenczy Rogožan May 18 '16 at 02:48
  • the last line "cd" command is not work, before run this script is in home folder "~", and run this script also in home folder. – KBKai May 18 '16 at 02:53
  • 1
    Again. It's working perfectly fine. Unless you describe how it really behaves (error messages etc.), nobody can help you. As I wrote "it's not working" is a bullsh*t. – David Ferenczy Rogožan May 18 '16 at 02:58
  • 1
    OK, so now execute it `source test.sh` and you'll find out that it actually works. Check the Jeff's answer to find out why it behaves how it behaves. – David Ferenczy Rogožan May 18 '16 at 02:59
  • Yes like @ Dawid Ferenczy said , it works prefectly... try this from the terminal : set -x ; t=$(date +%F-%H%M%S) ; mkdir -p $t && cd $t && pwd – z atef May 18 '16 at 03:23
  • 1
    Unless you use the `.` (dot) or `source` command on the script, the change of directory occurs in a sub-shell that exits immediately. The parent shell is unaffected, but the `cd` _does_ work. This is a duplicate of probably a number of similar questions — the difficulty, as ever, will be finding a good duplicate. – Jonathan Leffler May 18 '16 at 03:36

1 Answers1

1

you're running a script with a subshell.

the problem with running cd inside a script is that it creates a subshell starting from that directory. when the script ends, so does your subshell, and you're back where you started when you ran the script. so it goes like this:

  1. start script
  2. subshell is created
  3. create directory
  4. change to directory
  5. end script
  6. end subshell
  7. back to starting point

you can prove that cd works in the script and that you're inside that directory while executing your script by leaving a mark.

t=$(date +%F-%H%M%S) 
mkdir $t
cd $t
echo "I'm here" > inside.txt

now when the script ends, you'll be back where you started the script. but if you cd to the directory, and run ls, then you'll see the file you made.


as @Dawid Ferenczy points out, you can actually source this script into your current shell instead of running a subshell, such as:

source test.sh
Jeff Puckett
  • 37,464
  • 17
  • 118
  • 167
  • It depends on how you execute the script. If you `source` it, it'll use the current shell and after the script finishes you'll be in the newly created directory. – David Ferenczy Rogožan May 18 '16 at 02:57
  • @DawidFerenczy look at the OP's screen shot, they are running it with `sh` – Jeff Puckett May 18 '16 at 02:58
  • You actually finished asking the question and answered it. But you should use double quotes. Hes testing shell scripts as root, can't be too careful. – Argonauts May 18 '16 at 02:59