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

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.

Chris Davies
  • 116,213
  • 16
  • 160
  • 287