Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

fish shell. How to check if a variable is set/empty?

Tags:

shell

fish

How can I do something like

set variable (some_command_that_may_return_a_string)
if [ variable is set ]
    do_something

and inversely how do I check if the variable is empty?

like image 992
Adham Zahran Avatar asked Dec 10 '17 20:12

Adham Zahran


People also ask

How do you know if a variable is blank?

To find out if a bash variable is empty: Return true if a bash variable is unset or set to the empty string: if [ -z "$var" ]; Another option: [ -z "$var" ] && echo "Empty" Determine if a bash variable is empty: [[ ! -z "$var" ]] && echo "Not empty" || echo "Empty"

How do you set a variable in a fish shell?

To give a variable to an external command, it needs to be “exported”. Unlike other shells, fish does not have an export command. Instead, a variable is exported via an option to set , either --export or just -x .

Where is Fishrc?

fishrc doesn't exist The settings that would normally be written to a ~rc file are instead located in the config. fish file at ~/. config/fish . This means the same Bash-style syntax can't be used, so you will have to use the previously written descriptors unique to Fish.

Where is fish config?

The configuration file runs at every login and is located at ~/. config/fish/config. fish . Adding commands or functions to the file will execute/define them when opening a terminal, similar to .


3 Answers

  • set -q var (note the missing "$" - this uses the variable name) can be used to check if a variable has been set.

  • set -q var[1] can be used to check whether the first element of a variable has been assigned (i.e. whether it is non-empty as a list).

  • test -n "$var" [fn0] (or [ -n "$var" ]) can be used to check whether a variable expands to a non-empty string (and test -z is the inverse - true if it is empty).

These will be true/false in slightly different circumstances.

When no set var has been performed at all (and it has not been inherited from the parent process), set -q var, set -q var[1] and test -n "$var" will be false, test -z "$var" will be true.

When something like set var has been done (without any additional arguments), set -q var will be true, set -q var[1] will be false.

When something like set var "" has been done, both set versions will be true.

When something like set var "somestring" (or even set var "" "" [fn1]) has been done, the sets will be true and test -z "$var" will be false.


[fn0]: You never want to use test (or [) without quoting the variable. One particularly egregious example is that test -n $var will return true both if the variable contains something and if it is list-empty/unset (no set at all or set var without arguments). This is because fish's test is one of the few parts that follow POSIX, and that demands that test with any one argument be true. Also it does not handle lists properly - test -n $var will have weird results if var has more than one element.

[fn1]: This is because a list will be expanded as a string by joining the elements with spaces, so the list consisting of two empty strings will expand to " " - one space. Since that isn't empty, test -z returns false.

like image 120
faho Avatar answered Oct 18 '22 23:10

faho


@faho provided an awesome answer but I thought it would be easier if there's a code example.

function if-test-no-arg
  set var

  if set -q var
    # true
  end

  if set -q var[1]
    # false
  end

  # Always put quotation marks
  if test -n "$var"
    # false
  end

  if test -z "$var"
    # true
  end
end

function if-test-empty-arg
  set var ""

  if set -q var
    # true
  end

  if set -q var[1]
    # true
  end

  if test -n "$var"
    # false
  end

  if test -z "$var"
    # true
  end
end

function if-test-valid-arg
  set var "hello"

  if set -q var
    # true
  end

  if set -q var[1]
    # true
  end

  if test -n "$var"
    # true
  end

  if test -z "$var"
    # false
  end
end

function if-test-no-set
  if set -q var
    # false
  end

  if set -q var[1]
    # false
  end

  if test -n "$var"
    # false
  end

  if test -z "$var"
    # true
  end
end
like image 33
Sanghyun Lee Avatar answered Oct 18 '22 22:10

Sanghyun Lee


The modern version of fish shell (3.5.1) has string commands.

  • string length --quiet $var checks that the variable is not empty
  • not string length --quiet $var checks that the variable is empty
like image 1
Valery Viktorovsky Avatar answered Oct 19 '22 00:10

Valery Viktorovsky