It's FOSS

Fixing "zsh: bad assignment" error in Linux

Abhishek Prakash

The other day I was trying to create an alias in Linux for repetitive commands. An alias is a name that is translated as another name or command (or a set of commands).

So, I tried to create the alias in the following manner:

And it threw me the following error:

If you are a regular user of the Linux command line, you must have identified the error on the previous command. But I was preoccupied with my program in C++ and I did not notice the obvious error here.

In fact, I thought it to be an error with the way I used the combination of error for the alias. So, I fiddled for a couple of minutes and just to make sure what I was doing wrong, tried this command:

Now, I was certain that there was no error with the commands this time but I git the same result as above:

And that’s when I realized my mistake. You see, I have been working a lot with C++ and was following the standard of using spaces before and after the assignment operator (=). And that is what I used here as well. And shell does not like the wastage of “space”.

I removed the extra white spaces before and after the = and voilà! There it worked like a charm.

In fact, the same error can be encountered with the export command as well or any other variable assignments in the shell. There should not be spaces before and after equals sign.

This taught me a lesson to not waste white space while dealing with shell scripts and Linux commands. It’s not the same as writing programs in other languages.

I would add this tiny learning lesson to my list of things to know about the Linux terminal.

zsh bad assignment

I hope you would not have to waste your time with this problem if you mind those spaces before and after the equals sign.

Abhishek Prakash

Created It's FOSS 11 years ago to share my Linux adventures. Have a Master's degree in Engineering and years of IT industry experience. Huge fan of Agatha Christie detective mysteries 🕵️‍♂️

How To Fix Ubuntu Update Error: Problem With MergeList

How to fix "repository is not valid yet" error in ubuntu linux, how to use nightlight feature in linux mint to save your eyes at night, fixing 'shell script opening in text editor' in ubuntu and other linux, enabling bluetooth on arch linux, it's foss.

Making You a Better Linux User

It's FOSS

Great! You’ve successfully signed up.

Welcome back! You've successfully signed in.

You've successfully subscribed to It's FOSS.

Your link has expired.

Success! Check your email for magic link to sign-in.

Success! Your billing info has been updated.

Your billing was not updated.

Dey Code

[Fixed] Command Line zsh bad assignment error when setting an Alias – Alias

Photo of author

Quick Fix: When setting an alias, ensure there are no spaces around the = sign. For example, instead of alias foo = bar , use alias foo=bar .

The Problem:

When attempting to set an alias in the zsh terminal using the command alias pip = 'python3 -m pip' , the user encounters the error zsh: bad assignment . The same error occurs when the user tries to add the same line of code to their .zshrc file and refreshes their terminal.

The Solutions:

Solution 1: remove spaces around the equal sign.

The error is caused by spaces surrounding the = sign in the alias definition. To fix it, remove the spaces around the = , like this:

Solution 1: Add single quotes to the alias definition

In the command you’re using to define the alias, you’re missing single quotes around the value you’re assigning to the alias. The correct syntax is:

With the single quotes, the shell will correctly interpret the value you’re assigning to the alias, and the error should disappear.

[Fixed] Angular 4 – Failed: Can't resolve all parameters for ActivatedRoute: (?, ?, ?, ?, ?, ?, ?, ?) – Angular

Cannot infer type arguments for ResponseEntity<> – Java

© 2024 deycode.com

Search code, repositories, users, issues, pull requests...

Provide feedback.

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly.

To see all available qualifiers, see our documentation .

  • Notifications

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement . We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Error: ZSH_HIGHLIGHT_STYLES: assignment to invalid subscript range #359

@Lordanakun

Lordanakun commented Sep 2, 2016

@phy1729

phy1729 commented Sep 2, 2016

Sorry, something went wrong.

@danielshahaf

danielshahaf commented Sep 2, 2016 via email

  • 👎 28 reactions
  • 😕 2 reactions

@Lordanakun

flamendless commented Jul 11, 2017

Danielshahaf commented jul 11, 2017, flamendless commented jul 12, 2017, danielshahaf commented jul 12, 2017.

@hamedarman

hamedarman commented Jul 26, 2017

@TheBabu

TheBabu commented Aug 4, 2017

@saikiran-siriki

saikiran-siriki commented Sep 30, 2017

Danielshahaf commented sep 30, 2017, danielshahaf commented sep 30, 2017 via email, danielshahaf commented oct 1, 2017 via email, thebabu commented oct 4, 2017.

@AaronFlower

AaronFlower commented Mar 18, 2018

  • 👍 13 reactions
  • ❤️ 6 reactions

danielshahaf commented Mar 18, 2018

@bkjoel

bkjoel commented Apr 19, 2019

  • 👍 3 reactions

@mahiki

mahiki commented Apr 19, 2021

Danielshahaf commented apr 19, 2021.

@Sidebook

No branches or pull requests

@phy1729

Stack Exchange Network

Stack Exchange network consists of 183 Q&A communities including Stack Overflow , the largest, most trusted online community for developers to learn, share their knowledge, and build their careers.

Q&A for work

Connect and share knowledge within a single location that is structured and easy to search.

Why does echo “$USER:staff” throw zsh: bad substitution?

Puzzled because echo "PATH=$PATH:/usr/local/sbin" doesn’t (thought it had something to do with : ).

Also, in Bash, both commands work as I expected.

sunknudsen's user avatar

  • Hint: foo=bar; echo "$foo:s/r/X/" –  Kamil Maciorowski Mar 16, 2021 at 13:02
  • @KamilMaciorowski Interesting... is that sed string substitution? So why does the $PATH command work? –  sunknudsen Mar 16, 2021 at 14:42
  • @glennjackman I believe I fixed the typo. –  sunknudsen Mar 16, 2021 at 14:45
  • 1 Because :/ is different than :s . I don't know Zsh good enough to write a decent answer though. –  Kamil Maciorowski Mar 16, 2021 at 14:47
  • @KamilMaciorowski Ok... I think I get it now... :s is interpreted as string substitution. –  sunknudsen Mar 16, 2021 at 14:48

Because the :s after $USER is interpreted as an expansion modifier . You can see this clearly if you do the following:

And as you can see from the list above, :/ is not an expansion modifier.

Is it recommended to always use ${PATH} , ${USER} , etc... then?

No, it's usually fine to just use $USER , but sometimes, as you can see, it is required to use ${USER} . :)

However, regarding the code in your question, I can give you two other recommendations to use in Zsh:

  • Use $path instead of $PATH and
  • use print instead of echo .

Marlon Richert's user avatar

  • Yes, thanks for helping out Marlon! –  sunknudsen Jul 12, 2022 at 11:09

You must log in to answer this question.

Not the answer you're looking for browse other questions tagged zsh ..

  • The Overflow Blog
  • How do you evaluate an LLM? Try an LLM.
  • Why configuration is so complicated
  • Featured on Meta
  • New Focus Styles & Updated Styling for Button Groups
  • Upcoming initiatives on Stack Overflow and across the Stack Exchange network
  • Google Cloud will be Sponsoring Super User SE

Hot Network Questions

  • Book about a robotic probe comes to Earth and asks to be destroyed
  • Success of Aggressive Rebuttal Strategy
  • Create concentric scaled rings with individual noise translation to each instance
  • What is this vegetable?
  • Looking for a SF story with Checkpoint Charlie and a male scientist disguised as a woman
  • What options do I have for latex-free interior house paint?
  • What happens if you own a stock and the firm becomes liable about something
  • How do Biblical inerrantists explain disagreements about the interpretation of the Bible?
  • How to use the penpos/penstroke inside a parametrized macro?
  • Why doesn't Israel withdraw from the territories occupied during the Six-Day War of 1967?
  • I am new to Blender, I want to create this certain shape of a shampoo bottle, any help is appreciated!
  • Embarrassment at work caused by a supervisor
  • Can you have a planet as bright as Venus orbiting at 1.0 AU with no significant atmosphere?
  • Can you tile a 25 x 25 square with a mixture of 2 x 2 squares and 3 x 3 squares?
  • Do I need permissions to list companies using my library?
  • Is real-time processing a synonym for streaming?
  • Why is "the same" always with "the", but "identical" without it?
  • Perturbation of zeros of an entire function of exponential type
  • Can street names be normalized to single form?
  • How can I reserve a TGV seat on a Germany-Switzerland ticket purchased via Deutsche Bahn?
  • What is the reason for the difference between Eloi and Eli?
  • Why don't airports use different radio frequencies/channels for each plane to prevent communications from interfering with each other?
  • What are some tracing disassemblers for the Z80
  • How do you stay stable when landing/ Taking off on an F-35 during a VTOL landing/Takeoff?

zsh bad assignment

Stack Exchange Network

Stack Exchange network consists of 183 Q&A communities including Stack Overflow , the largest, most trusted online community for developers to learn, share their knowledge, and build their careers.

Q&A for work

Connect and share knowledge within a single location that is structured and easy to search.

Cannot export variable thanks to a script with zsh

I have written a script export-zsh to set new variables in zsh

Errors occur if I run it

When I execute each command individually, it lead to no errors:

when I run source ~/.zshrc , there are no errors, so I don't think it should be directly related to my ~/.zshrc content but I give it below to be sure.

user123456's user avatar

You script is executed by Bash. Reading your .zshrc with Bash doesn't work but gets you errors for all the Zsh specific stuff.

Your script runs in its own subshell. As settings, environment variables and such are not exported back to the calling shell ascript will not work here. Use a function instead and put it in your .zshrc :

Florian Diesch's user avatar

You must log in to answer this question.

Not the answer you're looking for browse other questions tagged zsh ..

  • The Overflow Blog
  • How do you evaluate an LLM? Try an LLM.
  • Why configuration is so complicated
  • Featured on Meta
  • New Focus Styles & Updated Styling for Button Groups
  • Upcoming initiatives on Stack Overflow and across the Stack Exchange network
  • AI-generated content is not permitted on Ask Ubuntu
  • Let's organize some chat workshops

Hot Network Questions

  • How bright would it be at night on my planet?
  • Odds Ratios paradox? Pooled OR inconsistent with subgroup ORs
  • Book about a robotic probe comes to Earth and asks to be destroyed
  • How to determine the number of Multi Layer Insulation layers for a satellite?
  • How to avoid accidentally sharing proprietary information when working for a competitor of a former employer?
  • Implicit licensing when contributing to an open source project
  • Do I need permissions to list companies using my library?
  • In which US states are hush money payments illegal?
  • In the RL02 disk system, why does the “read” command take CHS coordinates, when there is also a separate “seek” command?
  • A question on Euler's totient function
  • Is there a good term for a 'paper trail' that actually consists of e-mail communication?
  • Stealth on Titan (ground vehicles against satellites)
  • Package maintainer pre-inst script `install` vs `upgrade`
  • For every sequence of nonempty open sets there is a disjoint sequence of nonempty open sets "below" it
  • What happens if you own a stock and the firm becomes liable about something
  • Terminology clarification about sample moments
  • Is anything other than metals soluble in molten metals/alloys?
  • Science Fiction boardgame.Two players (maybe more possible) Torus shaped spaceship gameboard You control reproducing aliens. Played 1986-1988
  • What materials and building techniques would a Late Medieval-Renaissance city use next to an ocean?
  • Can you tile a 25 x 25 square with a mixture of 2 x 2 squares and 3 x 3 squares?
  • Physiology of Ann Leckie's Radchaai from the "Ancillary" series
  • Paradox of Brier skill score of perfectly calibrated output?
  • Can two interfering light beams create radio waves?
  • What is the provenance of this photo of the Great Lakes from space?

zsh bad assignment

IMAGES

  1. [Solved] "zsh: bad assignment" error in Linux

    zsh bad assignment

  2. Fixing "zsh: bad assignment" error in Linux [Quick Tip]

    zsh bad assignment

  3. Unix & Linux: Bash script throws "assignment to invalid subscript range

    zsh bad assignment

  4. Unix & Linux: ZSH: how to escape the following command (variable

    zsh bad assignment

  5. 5 Solutions

    zsh bad assignment

  6. A Guide to the Zsh Completion with Examples

    zsh bad assignment

VIDEO

  1. Skull Fist

  2. Benjamin Franklin Time Traveler

  3. Tyler, The Creator & Kali Uchis See You Again

  4. La Mission Csupo

  5. Black Rob's Tragic Journey: From Platinum Success to Homelessness 💿🏠 #shorts

  6. Sabry & Basma

COMMENTS

  1. Command Line zsh bad assignment error when setting an Alias

    Open the file /Users/me/.zshrc [this is the file where you were trying to add the path for Python] Look for the line that is missing a '', and add that ''. [most likely it's going to be the line you added with Python], so look there first. Save your file and exit.

  2. [Solved] "zsh: bad assignment" error in Linux

    An alias is a name that is translated as another name or command (or a set of commands). So, I tried to create the alias in the following manner: alias my_short_command = "command 1; command 2 && command 3; command 4". And it threw me the following error: zsh: bad assignment. If you are a regular user of the Linux command line, you must have ...

  3. zsh报错: bad assignment!我搞定了你呢?

    mac使用ZSH后的各种问题解决1、安装maven 提示mvn command not found分析解决2、.zshrc 文件不存在,vim默认是新建?安装oh-my-zsh配置.zshrc3、什么brew 指令又command not found?安装Homebrew如果出现brew指令无法找到Homebrew基本使用4、安装zsh-autosuggestion插件出现超时?结语 1、安装 ...

  4. [Fixed] Command Line zsh bad assignment error when setting an Alias

    Quick Fix: When setting an alias, ensure there are no spaces around the = sign. For example, instead of alias foo = bar, use alias foo=bar.

  5. Why does ZSH local variable assignment fail?

    1. If you run your script with -x (both bash and zsh) you will see that the $ (...) code gets interpreted/parsed differently. This is because, as stated in the FAQ, word splitting in zsh is backwards/bash/sh incompatible.

  6. variable

    From the zsh manual regarding the typeset builtin (which local is a special case of): Unlike parameter assignment statements, typeset 's exit status on an assignment that involves a command substitution does not reflect the exit status of the command substitution.

  7. (eval):15 bad assignment when using '=' as alias #523

    This is similar to #263 but I get a different error: (eval):15: bad assignment As a result, all plugin aliases are gone. I'm using the zsh calc plugin, which aliases the =. When I remove this plugin, I get no errors and got all plugin al...

  8. Why does Zsh complain of my variable assignment as "command not found"?

    foo=\ bar. proper assignment; now the value of foo is bar (note the leading space) foo-x=bar. command foo-x=bar (because foo-x is not a valid name for a shell variable) This is not specific to Zsh. The POSIX shell ( sh) and POSIX-compliant shells behave this way. Zsh (while not being POSIX-compliant in general) also follows.

  9. error/warning "_zsh_highlight_main_highlighter:36: bad assignment" when

    The first issue is, that =foo.sh should get resolved first (absolute path of "foo.sh", if any), and then looked at by type.. The second minor issue appears to be that the suffix aliases setup by zsh-mime-setup should not get considered to be an alias, if they are a command/executable file maybe.

  10. Bash script throws "assignment to invalid subscript range" when running

    It sounds like you're asking zsh to interpret that script. You don't say what updateExpediaGitRepos is, but I suppose that alias sources that script, using the . or source builtin command. Those are the commands to tell the current shell interpreter to interpret code in a file, so the shebang (#! /bin/bash) is not relevant.The shebang is only used by the kernel when you try to execute a script.

  11. zsh: bad subscript for direct array assignment: 0

    zsh: bad subscript for direct array assignment: 0. Ask Question Asked 2 years, 2 months ago. Modified 2 years, 2 months ago. Viewed 968 times ... Bash script throws "assignment to invalid subscript range" when running from zsh. 0. Using a sed command to trim extra spaces,characters, and decimals stuck ...

  12. Why does assigning to $path break my $PATH in zsh?

    As most scripts get written for bash and zsh incorporates a lot of such small in-obvious differences which sometimes bastardize the usability or portability to other systems, I tend to declare bash as the interpreter in the shebang of scripts: #!/bin/bash or #!/usr/bin/env bash

  13. Bad substitution error in zsh shell

    13. zsh has different parameter substitution than Bash, which is documented in man zshexpn. It supports a variety of modifiers to expansion behaviour, which are put in parentheses before the variable name: ${(X)name}. The modifier to include array keys (including for associative arrays) is k: ${(k)array} expands to the list of keys in the array ...

  14. zsh command substitution fails when assigned to LINES

    4. The problem is the variable name. LINES is one of the variables with a conventional meaning. Its meaning is to convey the number of lines in a terminal. There is a similar variable COLUMNS. One of the jobs of an interactive shell is to update these two variables when the terminal is resized. Both bash and zsh do this.

  15. Error: ZSH_HIGHLIGHT_STYLES: assignment to invalid subscript range

    ~ /brackets-highlighter.zsh|32 error| bad substitution. This is the source code of brackets-highlighter.zsh: Define default styles.: ${ZSH_HIGHLIGHT_STYLES[bracket-error]:=fg=red,bold} ... moved the highlight styles assignment to after sourcing zsh-syntax-highlighting.zsh. So yeah. I think I should look into zsh package managers like antigen ...

  16. Why does echo "$USER:staff" throw zsh: bad substitution?

    Stack Exchange network consists of 183 Q&A communities including Stack Overflow, the largest, most trusted online community for developers to learn, share their knowledge, and build their careers.. Visit Stack Exchange

  17. ZSH: how to escape the following command (variable assignment)?

    Tour Start here for a quick overview of the site Help Center Detailed answers to any questions you might have Meta Discuss the workings and policies of this site

  18. bash

    .zshrc inconsistent type for assignment? Ask Question Asked 6 years, 8 months ago. ... Zsh: Command Not found : mongo After trying to install mongodb 4.2 using brew. 1. zsh: command not found: mongo. 0. MongoDB , zsh: command not found: mongo. 2. MacOS zshrc Bad Math Expression operator expected. Hot Network Questions Tried to use cig lighter ...

  19. Cannot export variable thanks to a script with zsh

    Reading your .zshrc with Bash doesn't work but gets you errors for all the Zsh specific stuff. Your script runs in its own subshell. As settings, environment variables and such are not exported back to the calling shell ascript will not work here.