Swiftorial Logo
Home
Swift Lessons
Tutorials
Learn More
Career
Resources

Fish: Shell Scripting Language

Introduction to Fish

Fish (Friendly Interactive Shell) is a user-friendly and feature-rich shell for Unix-like operating systems. It aims to provide a more modern and intuitive scripting experience compared to traditional shells like Bash.

Basic Syntax and Usage

Fish scripting syntax is designed to be more human-readable and intuitive. Here’s a simple example of a Fish script:


#!/bin/fish

# This is a comment
echo "Hello, World!"
                

In this example:

  • #!/bin/fish specifies the interpreter (Fish) for the script.
  • # This is a comment is a comment line.
  • echo "Hello, World!" prints "Hello, World!" to the standard output.

Variables and Data Types

Fish supports variables and different data types, making it versatile for scripting tasks:


# Variable declaration and assignment
set NAME "Alice"
set AGE 25

# Print variables
echo "Name: $NAME"
echo "Age: $AGE"
                

Variables in Fish are loosely typed and can hold strings, integers, and lists.

Control Structures

Fish includes standard control structures for conditional execution and looping:


# Conditional statement
if test $AGE -ge 18
    echo "Adult"
else
    echo "Minor"
end

# Looping example
for i in (seq 1 5)
    echo "Count: $i"
end
                

Fish simplifies scripting with clean syntax for conditions and loops.

Functions

Fish supports functions for modular code organization and reuse:


# Function definition
function say_hello
    set name $argv[1]
    echo "Hello, $name!"
end

# Function call
say_hello Bob
                

Functions in Fish are defined using the function keyword and support arguments through $argv.

Conclusion

Fish (Friendly Interactive Shell) provides a modern and user-friendly environment for shell scripting. Its intuitive syntax and powerful features make it suitable for both interactive use and scripting tasks.