Why can’t I add a custom function to my bashrc without getting a syntax error?

0
2
Asked By Techie1234 On

I'm trying to add a custom function to my bashrc file to modify the behavior of the 'ls' command. Here's what I'm trying to add:

```bash
ls () {
if [[ "$*" == *"--no-details"* ]]; then
local args=("${@/--no-details/}" )
eza -l --no-permissions --no-filesize --no-user --no-time "${args[@]}"
else
eza -l "$@"
fi
}
```

However, when I save the file and source it, I encounter this error:

```bash
bash: /home/vrin/.bashrc: line 19: syntax error near unexpected token `('
bash: /home/vrin/.bashrc: line 19: `ls () {'
```

Does anyone know why this is happening? I've seen similar functions online that use the same syntax. Any tips would be really helpful, thanks in advance!

1 Answer

Answered By CodeWizard88 On

It looks like you might have an alias for `ls` that’s being picked up while trying to redefine it as a function. When you define a function with the same name as an existing alias, it can mess up the syntax. You can check if there's an alias for `ls` by running `type -a ls`. If there is, make sure to run `unalias ls` before sourcing your bashrc to clear it out.

CuriousCoder47 -

I remember removing that color_auto alias while tweaking my bashrc, but I didn't unalias it. That explains the issue! Thanks for the help!

Related Questions

LEAVE A REPLY

Please enter your comment!
Please enter your name here

This site uses Akismet to reduce spam. Learn how your comment data is processed.