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?

Jonathan
  • 103
  • 2

1 Answers1

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.

roaima
  • 107,089
  • 14
  • 139
  • 261
  • Thanks! I was using this solution, but I don't understand why I can't use the 'alias' option and other people can. – Jonathan Jan 20 '20 at 12:58
  • Like I said, your alias simply defined a function. – roaima Jan 20 '20 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. – roaima Jan 20 '20 at 16:46
  • Ok, perfect. Thanks very much for your help. – Jonathan Jan 21 '20 at 07:26