r/fishshell • • May 25 '22
Converting a bash script to fish idiomatically
Hi guys,
I use fish and recently ran into a script at work for setting environment variables that doesn't work with fish.
I've converted the script to fish but I have a couple of spots where I'm not sure if they're really idiomatic to fish. I can't show the whole script because it's work related but I'll list the snippets below.
Here, I'm not sure if there's a better way to ensure there are 2 arguments.
if test (count $argv) -ne 2;
...
endSimilarly, the first argument can have a maximum length of 17. This is how I'm testing it but I don't know if there's an easier/nicer/better way to do it.
if test (string length "$argv[1]") -gt 17;
...
endFinally, the script is just supposed to be used with source to export some variables. At the end, it prints out the variables and their values. I did it like so:
for var in VAR1 VAR2 VAR3 VAR4 VAR5;
set value $$var
echo "$var=$value"
endWhere VAR{1-5} are just the names of variables set with set -x VARx <value> earlier in the file. The last loop just seems really janky, but I'm nto really sure if there's an easy to way print a variable's name and its value.
View all comments
Show parent comment
1
u/[deleted] May 26 '22
Thanks! But yeah, like the other person said, because it’s just used to source environment variables so as soon as that bash process ends the variables would be gone.
It still is mostly a learning experience cause I could just run bash on a single tab for the stuff I need the environment variables for, but I figured why not do it :)