-2

I'm creating a shell script that will allow a back functionality on my shell. What would be a good way to change the terminal's directory in the script.

Just running something like this in my back.sh file doesn't work:

cd /

Even when I run the file like

source ./back.sh

A sample file that is not working properly:

#!/bin/bash

cd ~/Movies/

when I run source ./back.sh or source back.sh

Mogsdad
  • 42,835
  • 20
  • 145
  • 262
JonMorehouse
  • 1,251
  • 3
  • 14
  • 34
  • The variant with `source` should actually work. Perhaps some more information is needed. What is the exact content of `./back.sh`? How do you know it didn't work? – n. 1.8e9-where's-my-share m. Nov 09 '12 at 17:11
  • Could you elaborate more on what do you mean by back functionality. When you run the script it is run as separate process and sourcing should run the script in current shell and `cd` to `/` (at least that is what I see) – another.anon.coward Nov 09 '12 at 17:12
  • Hello, my back file is really complicated so here is a similar solution which is not working:`#!/bin/bash cd ~/Movies/ `\nAnd I run it with source ./back.sh – JonMorehouse Nov 09 '12 at 17:14
  • It does seem to work, why not add a `pwd` after `cd`ing and check for yourself. Maybe you can check if `~/Movies/` really exists like `test -d ~/Movies/ && cd ~/Movies/` – another.anon.coward Nov 09 '12 at 17:21
  • Hello, I tried this and when I run it like ./back.sh it will pwd the right directory, but when I run it like source ./test.sh it does nothing. Looks like the source command isn't working for it all... – JonMorehouse Nov 09 '12 at 17:25
  • Strange, it should. Try `. ./test.sh`, you can source using `.` – another.anon.coward Nov 09 '12 at 17:27
  • 1
    Hmm...strange because this works ... The problem is though, that I have always used . for my cd ../ command and wanted to keep this. What would cause this to be different from source? – JonMorehouse Nov 09 '12 at 17:30
  • Your sample seems to work when sourced, as n.m. commented. Perhaps, you don't have a ~/Movies directory? – Hai Vu Nov 09 '12 at 17:32
  • the directory works... the script works when running . ./test.sh but not when running source ./test.sh – JonMorehouse Nov 09 '12 at 17:33
  • 2
    If by *back functionality*, you meant *the ability to cd back to previous directory*, check out a) `cd -`, and b) `pushd` and `popd` commands. – Hai Vu Nov 09 '12 at 17:34
  • How do you know it's not working? Can you describe what you expect and what you actually get? – n. 1.8e9-where's-my-share m. Nov 09 '12 at 20:14

1 Answers1

1

Here are my 2 cents about the difference between . and source builtins: There isn't any.

Since you mentioned that you use . for cd ../, make sure that the proper . and source are executed and are really the same thing:

user@host> type source
source is a shell builtin     
user@host> type .
. is a shell builtin

Other than that /bin/bash --version may turn out helpful, too.

CRABOLO
  • 8,495
  • 38
  • 39
  • 67
Todor
  • 311
  • 1
  • 3