Skip to content
TheChatArchiveThe Chat Archive
Geminigemini-2.5-pro-preview-05-06· GoogleHugging Face LMArena Human Preference 140K

In the linux shell (zsh), I would like to pass an argument to an environment variable to…

From a Chatbot Arena battleThe same person sent this prompt to two anonymous models at once. Compare it with Amazon Novaamazon.nova-pro-v1:0’s answer.
  1. User

    In the linux shell (zsh), I would like to pass an argument to an environment variable to become active when the shell starts. The variable is EDITOR and I would like to set it to "emacs -nw", so I set it like this EDITOR="emacs -nw". But when I enter $EDITOR from my terminal, it says: "command not found: emacs -nw". Without "-nw" there is no problem. How do I set an environment variable to contain an argument?

  2. Gemini Assistant

    You've hit a common point of confusion with how the shell interprets commands and variables.

    The Problem with $EDITOR directly in the terminal

    When you type $EDITOR in your terminal and press Enter, the shell performs variable expansion. If EDITOR="emacs -nw", then $EDITOR expands to the literal string emacs -nw. The shell then tries to find a single command named emacs -nw (with the space as part of the command name). Since there's no such command (the command is emacs and -nw is an argument), you get "command not found: emacs -nw".

    When EDITOR="emacs", $EDITOR expands to emacs. The shell finds the command emacs and runs it. This works.

    How Programs Use EDITOR

    Programs that use the EDITOR environment variable (like git, crontab -e, fc, etc.) are smarter. They typically:

    1. Read the value of the EDITOR variable.
    2. Split the string by spaces.
    3. Take the first part as the command to execute (emacs).
    4. Take the subsequent parts as arguments to that command (-nw).

    So, setting EDITOR="emacs -nw" is correct for these programs.

    How to Set It Correctly (and Where)

    1. The setting is correct:

      export EDITOR="emacs -nw"
      

      The export command makes the variable available to child processes (which is what you want when another program needs to use EDITOR).

    2. Where to put it: For Zsh, the best place to set environment variables that should be available to all processes (including graphical applications started from the shell environment and non-interactive shells) is ~/.zshenv. Add this line to your ~/.zshenv file:

      export EDITOR="emacs -nw"
      

      If you also want to set VISUAL (often preferred, with EDITOR as a fallback), you can do:

      export VISUAL="emacs -nw"
      export EDITOR="$VISUAL" # Or set EDITOR to something else like "vi" if preferred as a fallback
      

      VISUAL is generally for full-screen editors, while EDITOR was traditionally for line editors. Most modern applications will check VISUAL first, then EDITOR.

    3. Apply the changes: Either source the file:

      source ~/.zshenv
      

      Or simply open a new terminal window/tab.

    How to Test If It's Working (the right way)

    Don't just type $EDITOR. Instead, use a command that relies on the EDITOR variable:

    • fc (Fix Command): This is a shell builtin that usually opens the last command in your $EDITOR.

      # Type some dummy command first if your history is empty or you want a specific one
      echo "this is a test"
      fc
      

      This should open emacs -nw with the echo command in it.

    • crontab -e: Tries to edit your crontab.

      crontab -e
      

      (You can just exit emacs without saving if you don't want to change your crontab).

    • git commit --amend (if you're in a git repo): If you're in a Git repository and have made a commit, this will try to open the commit message in your editor.

    If you want to run $EDITOR directly in the shell (for testing/convenience)

    While not how programs use it, if you want to execute the command stored in EDITOR directly from your interactive shell prompt, you have a couple of options:

    1. eval (use with caution): eval tells the shell to evaluate the string as a full command line.

      eval $EDITOR somefile.txt
      

      This would expand to eval emacs -nw somefile.txt, and the shell would then correctly parse and execute emacs -nw somefile.txt. Be careful with eval if the variable could come from an untrusted source, as it can execute arbitrary code. For EDITOR you set yourself, it's generally fine.

    2. Shell function/alias (better for interactive use): If you frequently want to type a short command to launch emacs -nw, create an alias or function in your ~/.zshrc:

      # In ~/.zshrc
      alias enw='emacs -nw'
      # or a function
      # enw() { emacs -nw "$@" }
      

      Then just type enw somefile.txt.

    In summary: Your EDITOR="emacs -nw" setting is correct for its intended purpose. The way you were testing it ($EDITOR directly) was misleading. Use commands like fc to verify it. Place the export EDITOR="emacs -nw" line in your ~/.zshenv.

Responses were produced in different, uncontrolled contexts (system prompts, settings, prior turns). This is not a benchmark.