0

I have the following code in my .bashrc:

c() { cd "$@" && ls -a ; }
alias cdd="c ~/Downloads"
alias r=". ~/.bashrc"

When I load a new terminal, cdd doesn't work before I do the r command. But after doing so, it works as intended. Why is this?

I asked this same question on Stack Overflow a few months ago, but none of the responses were able to give me a working solution. Here is the link to my dotfiles if that will be of any use.

EDIT type -a c cdd r produces:

c is aliased to `clear'
c is a function
c () 
{ 
    cd "$@" && ls --color=auto -a
}
cdd is aliased to `c ~/Downloads'
r is aliased to `. ~/.bashrc'

Based on this information, I changed the c function to d, and it works. That being said, is there any way I could not map the clear function to c as I already have alias cs='printf "\033c"' for that)? I looked in .bashrc but couldn't find anything with the word "clear".

Kevin
  • 137
  • Run PS4=' ${BASH_SOURCE}:${LINENO} ' bash -lixc exit |& grep clear and add the output, please. – muru Jan 30 '18 at 05:32
  • I don't seem to be getting an output. – Kevin Jan 30 '18 at 05:33
  • to remove the alias of c do unalias c, that's it. To find where that is being defined use muru command and post output: PS4=' ${BASH_SOURCE}:${LINENO} ' bash -lixc exit |& grep clear –  Jan 30 '18 at 05:46
  • Probably comes from https://github.com/Bash-it/bash-it/blob/956b3e417fa1a4066fd9248e9a01fedbc4544dfc/aliases/available/general.aliases.bash#L39 – muru Jan 30 '18 at 05:54
  • Ohh. Strange though, I don't have that line in my .bashrc yet it's still aliased with every terminal launch. – Kevin Jan 30 '18 at 05:56
  • Eh? You clearly are sourcing bash-it in your bashrc. It probably sets some environment variable that prevents it from being sourced again, so it didn't show up in the bash command I posted above. – muru Jan 30 '18 at 06:02

1 Answers1

0

If the c is aliased to clear but not in your own shell initialization files, then it's most likely done in the system-wide initialization files under /etc. Check, for example /etc/bash.bashrc (or see your bash manual for what files are used, this may differ between systems depending on how bash was built).

No matter where c is aliased to clear, you may unalias c before defining it as a function with

unalias c

You may also consider using $HOME in place of ~. This is due to $HOME being more descriptive (and you don't actually need to type it out since it's part of an alias, so you don't "lose" anything), and that it behaves as a variable whereas ~ does not. See Why doesn't the tilde (~) expand inside double quotes?

Kusalananda
  • 333,661
  • Weird. I added unalias c, but I get bash: unalias: c: not found even though it shows that it's still assigned to clear when I do type -a c cdd r. – Kevin Jan 30 '18 at 22:55
  • @Kevin Then look further down in your .bashrc file to find where the c alias gets defined or where you source a file that defines it. – Kusalananda Jan 31 '18 at 06:23