Day 48 of #100DaysOfLearning
fish shell

Day 48 of #100DaysOfLearning

What is the shell that you guys are using, is it Zsh? Is it Zsh or Bash? There was a time when I used ksh (Korn shell) a lot.

These days it is different. The standard shell I use these days on Mac and Windows is the fish shell.

I just prefer to use it, and this is by no means a recommendation for everyone, so if you are interested, use it??

What is Fish shell?

Fish, which stands for "Friendly Interactive Shell," is a command-line shell designed for Unix-like operating systems. It aims to be user-friendly, interactive, and provide a modern command-line experience.

Key features if the Fish shell

Here are some key features and aspects of the Fish shell:

  1. Syntax Highlighting: One of Fish's notable features is syntax highlighting. When you type commands, Fish colors them to make it easier to distinguish between different elements such as commands, arguments, and options. This feature helps users spot errors and understand the structure of their commands more easily.
  2. Auto-Suggestions: Fish provides automatic suggestions for commands and arguments as you type. These suggestions are based on your command history and available commands, making it quicker to enter commands without typing the entire text.
  3. Powerful Tab Completion: Fish has robust tab completion, making it efficient to navigate and complete commands. It supports not only completing command names but also options and arguments. Tab completion in Fish is context-aware and dynamically adapts based on the command syntax.
  4. Command Syntax: Fish has a simplified and consistent syntax. While traditional shells like Bash or Zsh may have more complex syntax rules, Fish aims to make commands more straightforward and predictable.
  5. Built-in Help: Fish provides extensive built-in help that you can access easily. The help system provides information about commands, options, and syntax. You can use the help command to get details on specific commands or topics.
  6. Scripting Language: Fish has its scripting language that differs from the syntax used in other shells like Bash or Zsh. While it's designed to be user-friendly, it might require adjustments for users familiar with other shell scripting languages.
  7. Configurability: Fish is highly configurable. Users can customize their prompt, configure environment variables, and modify other settings to tailor the shell to their preferences.
  8. Default Shell on Some Systems: While Bash is a commonly used default shell on many Unix-like systems, some systems, like some versions of macOS, come with Fish as the default shell for new users.

As I discussed earlier, it's important to note that while Fish is user-friendly and provides a great interactive experience, it may not be the best choice for all scenarios. Some users, especially those with extensive experience in other shells, might prefer the scripting capabilities and compatibility of more traditional shells like Bash or Zsh. Fish is often appreciated for its approachability, particularly for users new to the command line.

Key differences between Bash/Zsh and Fish

Syntax:

  • Bash and Zsh: Bash and Zsh have a more traditional and complex syntax. Commands and scripts often follow a specific structure, and the syntax may be less forgiving for beginners.
  • Fish: Fish has a simplified and consistent syntax, making it more user-friendly and approachable, especially for new users.

Auto-Completion:

  • Bash and Zsh: Both Bash and Zsh support auto-completion, but Fish's auto-completion is known for being more powerful and context-aware.
  • Fish: Fish provides advanced auto-suggestions and completion that adapt dynamically based on the context. It suggests not only commands but also options and arguments.

Syntax Highlighting:

  • Bash and Zsh: While some configurations and plugins can add syntax highlighting to Bash and Zsh, it's not a built-in feature and may require additional setup.
  • Fish: Syntax highlighting is a built-in feature in Fish, making commands visually distinct and helping users identify errors more easily.

Help System:

  • Bash and Zsh: Both Bash and Zsh have a built-in help system, but accessing and navigating it might not be as straightforward.
  • Fish: Fish has an extensive built-in help system that is easily accessible using the help command. It provides detailed information about commands, options, and syntax.

Configuration:

  • Bash and Zsh: Configuration in Bash and Zsh often involves modifying configuration files (e.g., .bashrc or .zshrc). It can be highly customizable, but the learning curve might be steeper.
  • Fish: Fish has a simpler and more centralized configuration system. Configuration is typically done through a single configuration file, and the syntax is more consistent and user-friendly.

Scripting Language:

  • Bash and Zsh: Bash and Zsh use a similar scripting language, making it easier for users familiar with one to switch to the other. They are well-suited for scripting tasks.
  • Fish: Fish uses its scripting language, which might be different from what users are accustomed to in Bash or Zsh. While it's designed to be more readable, users with existing Bash/Zsh scripts may need to make adjustments.

Default Shell:

  • Bash and Zsh: Bash is the default shell on many Unix-like systems. Zsh is widely used and can be installed as an alternative.
  • Fish: Fish is not as commonly set as the default shell on most systems, but it is gaining popularity, and some systems (e.g., some versions of macOS) use it as the default for new users.

Fish script sample

When you start using Fish, you may create various scripts. Here is a simple sample of a script.

The following is a sample of argparse that I often use.

argparse

The argparse command in Fish shell is a powerful tool for handling command-line arguments in scripts and functions. It simplifies the process of parsing options and arguments by providing a convenient syntax. Below, I'll break down some key aspects and provide an example.

Basic Usage:

To use argparse, you pass option specifications, followed by a --, and then the arguments to be parsed. Here's a basic example:

argparse 'h/help' 'n/name=' -- $argv        

In this example:

  • h/help: This specifies that both -h and --help are valid boolean flags. If either flag is used, _flag_h and _flag_help variables will be set accordingly.
  • n/name=: This specifies that both -n and --name are valid. It requires a value and can be used at most once. If the flag is seen, _flag_n and _flag_name will be set with the single mandatory value associated with the flag.
  • --: This separates the option specifications from the actual arguments.
  • $argv: This is the array of arguments to be parsed.

Option Specifications:

Each option specification consists of:

  • An optional alphanumeric short flag character, followed by a / if the short flag can be used or a - if it should not be exposed as a valid short flag.
  • An optional long flag name.
  • Nothing if the flag is a boolean or an integer flag.
  • = if it requires a value.
  • =? if it takes an optional value.
  • =+ if it requires a value and each instance of the flag is saved.
  • Optionally, ! followed by Fish script to validate the value.

Sample

function greet
    # Define option specifications
    argparse 'n/name=' 'a/age=?' -- $argv

    # Check if the name option is provided
    if set -q _flag_name
        set name $_flag_name
    else
        set name "Stranger"
    end

    # Check if the age option is provided
    if set -q _flag_age
        set age $_flag_age
    else
        set age "unknown"
    end

    # Display the greeting
    echo "Hello, $name! You are $age years old."
end        
greet               # Hello, Stranger! You are unknown years old.
greet -n Alice      # Hello, Alice! You are unknown years old.
greet -n Bob -a 25  # Hello, Bob! You are 25 years old.        

Next Action

I am not yet confident enough to introduce Fish to many people because I do not fully understand and use it myself.

I would like to become more proficient in Fish so that I can convey the joy of using it.

By the way, if you are interested in Fish, you should try using squid oh-my-fish.

oh-my-fish/oh-my-fish: The Fish Shell Framework (github.com)


要查看或添加评论,请登录

Shinya Yanagihara的更多文章

  • Day 100 of #100DaysOfLearning

    Day 100 of #100DaysOfLearning

    I have mixed feelings about it, as if it was long and short. This is finally the 100th activity that I started with the…

    1 条评论
  • Day 99 of #100DaysOfLearning

    Day 99 of #100DaysOfLearning

    What a surprise! I found myself on the 99th day of the 100Days of Learning activity. Continuation is power, indeed.

  • Day 98 of #100DaysOfLearning

    Day 98 of #100DaysOfLearning

    How do you take notes when you study? There are some note-taking systems and techniques, such as Cornell note-taking…

  • Day 97 of #100DaysOfLearning

    Day 97 of #100DaysOfLearning

    Today is the fourth day of setting up a Windows environment. Today I finally get to set up my long-awaited development…

  • Day 96 of #100DaysOfLearning

    Day 96 of #100DaysOfLearning

    I am sure you are all aware that open source also has a license. I knew that, but I always managed my GitHub…

  • Day 95 of #100DaysOfLearning

    Day 95 of #100DaysOfLearning

    Today is the third day of building a new PC environment. Today I was mainly working on the configuration of Visual…

    2 条评论
  • Day 94 of #100DaysOfLearning

    Day 94 of #100DaysOfLearning

    It is no exaggeration to say that Windows is now Linux. I'm sure some of you don't know what I mean.

    2 条评论
  • Day 93 of #100DaysOfLearning

    Day 93 of #100DaysOfLearning

    In order to make a clean break with the past, I did a clean install of Windows 11 and began to create a clean…

  • Day 92 of #100DaysOfLearning

    Day 92 of #100DaysOfLearning

    Happy April Fool's Day! Today is April 1, which is April Fool's Day. Some of you may have been looking forward to April…

  • Day 91 of #100DaysOfLearning

    Day 91 of #100DaysOfLearning

    I actually haven't used a Mac since I left my last job and entered my career break period. I use Windows every day.

社区洞察

其他会员也浏览了