0

I'd like to have something like this - when I type "go", it would cd to a directory, fire "pwd" and "ls -a".

~ $ pwd
/home/ondra
~ $ go sketchbook

/home/ondra/sketchbook
.  ..  libraries

~ $ pwd
/home/ondra/sketchbook
~ $ 

First I tried with alias, it didn't work, now I have this, but it doesn't work either:

#!/bin/bash

cd $1
pwd
ls -a

It properly prints the info for the target directory, but does not change to it.

Looks like it works only within the script. Any way to do this?

MightyPork
  • 18,270
  • 10
  • 79
  • 133
  • @Ignacio that is nice, but that doesn't explain how can I do it. – MightyPork Nov 24 '14 at 08:24
  • 2
    This other duplicate's accepted answer might be helpful: http://stackoverflow.com/questions/874452/change-current-directory-from-a-script – Michael Burr Nov 24 '14 at 08:25
  • Did you read the answers? – Ignacio Vazquez-Abrams Nov 24 '14 at 08:27
  • Yes, it's all some tricks with alias, I want a real solution – MightyPork Nov 24 '14 at 08:38
  • Besides, if I use alias like "my script.sh && cd", my script won't receive the argument. That's not a solution. – MightyPork Nov 24 '14 at 08:43
  • 1
    Function is not a trick but the solution. Your problem is that all commands from your script are run within a new shell. When it ends you come back to the old one and its PWD. Another way is to source commands from the file: source go sketchbook or . go sketchbook. Actually it's described as another answer to the question mentioned above. You can even overwrite `cd` that way. – pawel7318 Nov 24 '14 at 10:33
  • Okay, I've achieved what I want by adding that function to .bashrc. Hacky but does the trick. – MightyPork Nov 24 '14 at 11:04

0 Answers0