0

I know that exists a previous question about this subject. But a moderator has delete my question in these thread, for this reason I ask here the same.

In my .bashrc I try with:

alias cl='$HOME/.cFolder.sh'

The content of .cFolder.sh is:

#!/bin/bash

cFolder() {
    cd $1 && pwd && ls;
}

cFolder $1

When I execute my new alias I get this:

 jonathan $ cl Documentos/myScripts/
/home/jonathan/Documentos/myScripts

arranque-fallido.log      firstInstall.sh    makeAllPacks.sh          ORIGINAL-installPegaso.sh  restoreBackup.sh  startInitialConfig.sh
configure-usb-install.sh  fixDatabase.sh     makeInstallationPack.sh  README.md          restoreData.sh    testZenity.sh
downloadDebs.sh       installAppPack.sh  myScripts-Functions.sh   recoverSystem.sh       setNetwork.sh     updateWeb.sh
 jonathan 

The list of files is correct. But it doesn't change my actual folder. The .cFolder.sh script runs in his own space.

The option:

alias cl='cFolder() { cd $1 && pwd && ls; }'

doesn't work for me. How can I fix this?

0

1 Answer 1

1

Your first attempt creates an alias to run a shell script that changes directory. Only, as explained in many places, you cannot change your current shell's directory from a subshell. So this doesn't work.

The second attempt creates an alias that defines a function. It doesn't run it, so this almost certainly isn't what you want.

Remove the aliases you've created, and put this into your .bashrc

cFolder() { cd "$1" || return; pwd; ls; }

The next time you start a shell you will have a new "command" called cFolder. No need for an alias. No need for a separate script.

4
  • Thanks! I was using this solution, but I don't understand why I can't use the 'alias' option and other people can. Commented Jan 20, 2020 at 12:58
  • Like I said, your alias simply defined a function. Commented Jan 20, 2020 at 16:42
  • Oh, and I don't see any successful use of an alias to do what you want. Due to the way aliases interpolate arguments (i.e. they don't), I'd be inclined to say it's not possible. Commented Jan 20, 2020 at 16:46
  • Ok, perfect. Thanks very much for your help. Commented Jan 21, 2020 at 7:26

You must log in to answer this question.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.