Fish Shell:
Concatenating strings
How to:
In Fish, slap strings together with spaces between or use string join
.
# Combine 'Hello' and 'World!' with a space
echo 'Hello' 'World!'
# Output: Hello World!
# Concatenate variables
set greet "Howdy"
set who "Partner"
echo $greet $who
# Output: Howdy Partner
# No-spaces concatenation with string join
set file "report"
set ext "txt"
string join '' $file '.' $ext
# Output: report.txt
Deep Dive
Concatenation’s been around since the dawn of programming. In Fish, string join
is cleaner than older methods, such as using echo
followed by string vars without quotes. This approach avoids subcommand overhead, which can be a performance win.
Alternatives include the use of printf
, which gives more formatting control but is a touch more complex for simple joining operations. Example:
set firstName "Ada"
set lastName "Lovelace"
printf "%s %s\n" $firstName $lastName
Fish’s string
command is part of a built-in string manipulation toolbox introduced to make text processing more straightforward. It’s not unique to Fish, but its inclusion as a built-in tool keeps things simple.