0
cdcl()          { vlog -source -lint +define+"${1:-DEBUG}" -sv "${2:-*.sv}"; }

Above is my fucnction, i defined it in .bashrc file. Below is my command line

% cdcl 'RANDOM' 'abc.sv'

This give me >> vlog -source -lint +define+RANDOM -sv abc.sv

Is there a way, where i can skip giving the 1st placeholder value and only give 2nd placeholder in command line.

4
  • 1
    If you mean you only want to pass one argument, you can use options (something like cdc1 --sv 'abc.sv') either coded yourself or using getopts. Otherwise, how would bash know which parameter you meant to pass?
    – pell
    Commented Feb 17, 2023 at 22:33
  • I want shell to consider default value(DEBUG) of 1st placeholder and it should interpret the argument i enter after cdcl as 2nd argument. How to achieve it?
    – Harsha
    Commented Feb 17, 2023 at 23:29
  • Rewrite your alias to take only one argument, the one that is currently $2. Hardcode the current $1 to DEBUG.
    – doneal24
    Commented Feb 18, 2023 at 3:00
  • aliases don't take arguments, they're a simple substitution of the alias name for the alias definition. If you need to handle arguments, write a function or a script, not an alias.
    – cas
    Commented Feb 18, 2023 at 4:52

3 Answers 3

2

You could check whether your function is called with exactly one parameter and treat that case differently:

cdcl() {
  if (($# == 1)); then
    vlog -source -lint +define+DEBUG -sv "$1"
  else
    vlog -source -lint +define+"${1:-DEBUG}" -sv "${2:-*.sv}"
  fi
}
0

Since the function uses the default value expansion with the colon, i.e. "${1:-DEBUG}" and not "${1-DEBUG}", an empty value will also trigger the use of the default value. You can pass an empty value with "" or '', so

cdcl "" abc.sv

should result in the function running

vlog -source -lint +define+DEBUG -sv abc.sv

If the function had used "${1-DEBUG}" instead, it'd only trigger the default value for an unset value, and it's not possible to have $1 undefined while $2 is defined. They have to come in order, the list can't have "holes" in it.

Not that passing an empty string like that is a pretty solution, and you might want to decide on e.g. using getopt within the function to have it take arguments like cdcl -d RANDOM -s abc.sv instead, again with the appropriate default values.

0

If I understand your question correctly you want to skip providing the first argument to the script so it uses RANDOM but without having to type it. You can revert the order of your arguments.

cdcl() {
  local f_pattern="${1:-*.sv}"
  echo "vlog -source -lint +define+${2:-DEBUG} -sv $f_pattern"
  # vlog -source -lint +define+"${2:-DEBUG}" -sv "$f_pattern"
}

I used echo there to print the command with the arguments it will use. If you are confident, remove the echo

1
  • Hello, thanks for ur response. I want shell to consider default value(DEBUG) of 1st placeholder and it should interpret the argument i enter after cdcl as 2nd argument. How to achieve it?
    – Harsha
    Commented Feb 17, 2023 at 23:30

You must log in to answer this question.

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