1

I wrote this script to be able to quickly go to n:th directory in an ls output:

#!/usr/bin/env bash
# cd to the nth directory in a list as produced by ls
cd $( ls | head -n$1 | tail -n1 )

I named it cde and made it executable (it's in my $PATH) so now I can use

. cde 3

to change to the 3:rd directory for example (ie I source it). Because of how bash creates subshells for scripts I cannot just execute it like

cde 3

since only the directory of the subshell is affected.

How would you do to get rid of having to write that extra dot and still get the desired behaviour?

I would have used an alias for this instead of a script, but I couldn't figure out how since I don't know how to pass arguments to aliases.

Anton
  • 365
  • 2
  • 12
  • 1
    "since only the directory of the subshell is affected" Can you please add more details about this in your answer? – gaganso Jul 11 '18 at 04:23
  • 1
    Added a sentence about subshells vs interactive shells (or is it better to call it "parent shell" or something else in this context?). Is that what you were going for, or is there anything else? – Anton Jul 11 '18 at 07:00

1 Answers1

3

Use a function instead of a script or an alias!

Functions are way more flexible than aliases, and does not create subshells like executing scripts do. Thus the change of directory is going to affect your interactive ("current") shell.

You can define a function to do this like so:

# cd to the nth directory in a list as produced by ls
function cde {
cd $( ls | head -n$1 | tail -n1 )
}

Put the fuction definition in your ~/.bash_aliases file (or some other file that gets sourced upon terminal startup, like ~/.bashrc), and you won't have to define it manually each session.

It will give you the desired behaviour.

Anton
  • 365
  • 2
  • 12