0

So, I'm trying to create an alias which will run a command after sleeping x seconds.

alias ls_sleep="sleep $1 && ls"

The problem is, $1 is being passed as string and things like $(($1)) or $(($1+0)) doesn't work.

Would it be possible to make it work in a single line without using a function?

2
  • Welcome, does Insert a user defined variable in an alias in Bash answer your question? Commented Feb 17, 2022 at 12:10
  • "The problem is, $1 is being passed as string " -- no it isn't, variables and positional parameters are pretty much always strings. Try it with alias ls_sleep="sleep $1; ls" instead and, see if you can spot what happens. Aliases aren't functions and they don't act like such. If you have some reason you can't use a function instead, you might want to think very hard how valid it is...
    – ilkkachu
    Commented Feb 17, 2022 at 12:12

1 Answer 1

2

You can’t do this with an alias, but you can do it in a single line with a function:

ls_sleep() { sleep "$1" && ls; }

Not the answer you're looking for? Browse other questions tagged .