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 export

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.

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.

Periodically source shell profile

I'm a zsh user, but this should apply to any shell in theory. When I edit my .zshrc to add a new alias, I have to source ~/.zshrc in all of my open terminal tabs/windows to be able to use the new alias. I'm wondering if there's some way to have my open shells periodically source my profile automatically.

  • .bash-profile

swrobel's user avatar

3 Answers 3

The most complex and most real-time solution is to write a user trap function in .zshrc which when the trapped signal is caught, sources .zshrc itself.

Within zsh, this can be seen now:

Now some change in .zshrc

Here comes the tricky part. Using inotify watching .zshrc or by a watcher script that examines the last modification of .zshrc a command is triggered that sends USR1 signal to all zsh processes of the user. Now I just ran it in another shell:

And the result is:

Volker Siegel's user avatar

  • Very interesting approach. However, I'd rather use killall -s USR1 zsh than this ps-awk-xargs construct. –  mpy Feb 20, 2016 at 9:12
  • Valid correction. Thx! –  user556625 Feb 20, 2016 at 9:14

How about aliasing the command you use regularly?

You may need to adapt this for zsh:

So when you vi anything, it will source ~/.zshrc

You could do this with cd or ls for example.

Another option would be to build it into your prompt:

Community's user avatar

  • Thanks for the suggestions! I've already built it into the command I use to edit my profile, but that only helps with the shell that I edited it from. Building it into my prompt seems more promising... Have you tried that? –  swrobel Feb 19, 2016 at 1:13
  • @swrobel No, I haven't tried it specifically, but it should work fine - you must escape the first $ otherwise it only runs the first time –  Paul Feb 19, 2016 at 1:22
  • Hmm, the export results in the following error and no prompt: /Users/swrobel/.zshrc:export:8: not valid in this context: ~.zshrc}u@h:w$ zsh: bad substitution –  swrobel Feb 19, 2016 at 1:58
  • Sorry, fixed the typos. @swrobel –  Paul Feb 19, 2016 at 2:29

You are absolutly right, this function might be constructed in any shell, and so since more than a quarter of century.

It is not, and from my point of vue here is why. Such a function could lead to a fatal termination of all shells running in case of a slightest error. Example: you have a code section for debugging which contains

and during editing session of your .zshrc you removed the code protecting this exit to make it the general rule. All your running zsh will end. You won't be able to start another one with the same userid to fix your .zshrc . You will have to use another account which is sudo enabled to fix the killer .zshrc .

Then the best practice is to always test a .zshrc in a newly created shell so as to be able to survive.

This missing function is a very wise one.

dan's user avatar

You must log in to answer this question.

Not the answer you're looking for browse other questions tagged bash shell zsh bashrc .bash-profile ..

  • The Overflow Blog
  • Supporting the world’s most-used database engine through 2050
  • Net neutrality is in; TikTok and noncompetes are out
  • Featured on Meta
  • Testing a new version of Stack Overflow Jobs

Hot Network Questions

  • Does weight of an aircraft affect its ability to pull G?
  • Literary work where people are permitted only a certain amount of words to speak
  • Numbers reminding me of cities
  • What are notable instances of contradictions and omissions in the Bible the Church of Jesus Christ of Latter-day Saints has identified?
  • What to check when chainsaw chain won't move freely?
  • Does Hebrews 13:2 teach that God's elect have become angels in disguise?
  • ImportRange Query Formatted Incorrectly
  • What are the consequences or risks of signing a document with a future date?
  • How best to translate "the tip of the iceberg"?
  • Golden ratio pattern in Sierpinski matrix eigenvalues
  • What is the rationale for the change of syscall calling convention in new Linuxes?
  • Expanding the extent of a raster while keeping original cell values
  • Value of describing game mechanics in a data file instead of hardcoding?
  • How do I separate these double exposed images?
  • Reasonable doubt of eye witness's need for glasses in 12 Angry Men
  • How does one research (pure) mathematics in a more inclusive manner?
  • Storing a chandelier by suspending it from the walls
  • How can I select all edges that form part of a square without a face via python?
  • Identify the more acidic hydrogen in 1-(methylsulfanyl)propane-2-thione
  • Hammer of Runic Focus
  • Does magic that quicken healing help with damage dealt by Heart's blood
  • Why is there bronze powder aboard blimps to be used as pyrotechnics?
  • Does damage taken from Warding Bond count as magical?
  • What's this nugget called in these tektro drop levers?

zsh bad assignment export

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.

How to make a permanent alias in oh-my-zsh?

In my .zshrc I tried to make a few aliases .I looked into a lot of places, but I couldn't find out a way that worked. I used this code below:

Then I wrote a command source ~/.zshrc . Still it didn't resolve the issue. I get error messages like zsh: command not found: j

Could anyone help me with any suggestions and let me know what am I doing wrong?

  • command-line

Adaephon's user avatar

  • 4 Judging from the (mutliple) comments and from the error I suppose each alias is actually set on a single line, correct? –  kos Apr 17, 2016 at 22:49

There must not be any whitespaces around between = and either alias name or alias definition:

BTW: If you are looking for a way to shorten directory names, I suggest looking into Named Directories and the AUTO_CD option instead of aliases:

This allows you to use ~w instead of ~/Documents/UoMWorkspace/Semester2 and ~j instead of ~/Documents/UoMWorkspace/Semester2/COMP17412 (or ~w/COMP17412 ). So cd ~j is identical to cd ~/Documents/UoMWorkspace/Semester2 . It also works as part of a path, e.g. cat ~j/somedir/somefile .

zsh will automatically cd to a directory if it is given as command on the command line and it is not the name of an actual command. e.g.

  • 1 As a further explanation why not use spaces before and after the equation sign:e.g. a = b : This will be considered as a command a with two parameters ( b and = ) and will throw an error: zsh: command not found: a . If you put alias before that ( alias a = b ), it will be somehow similar. –  Timo Oct 22, 2017 at 7:29
  • 1 Isn't that AUTO_CD cause your command history unreadable? –  nanono Apr 18, 2020 at 3:34
  • 2 @nanono That depends on what you mean by "unreadable". By itself AUTO_CD does nothing more than allow to switch into directories by only using their paths instead of using the cd command. Of course, if you make active use of this feature, it is not easy to tell from the history whether the command docs ran a executable of that name or if it was used to switch into a directory named "docs". –  Adaephon Apr 21, 2020 at 15:04
  • Special thanks for shortening directories names - I have been looking for it –  bora89 Jan 7, 2021 at 14:08

You must log in to answer this question.

Not the answer you're looking for browse other questions tagged command-line alias zsh ..

  • The Overflow Blog
  • Supporting the world’s most-used database engine through 2050
  • Net neutrality is in; TikTok and noncompetes are out
  • Featured on Meta
  • Testing a new version of Stack Overflow Jobs
  • Let's organize some chat workshops

Hot Network Questions

  • Is this the right way to shoot in the style of a Hollywood silent movie?
  • Why is construction required in geometry?
  • Is the supposedly fake prophecy of Mahdi that the Bene Gesserit implanted on Arrakis actually a real prophecy?
  • Can a pilot tell a controller they don't need IFR clearance?
  • Precise pronunciation of b, d and g
  • Why doesn't b = a; f[a_] := b; f[2] return 2?
  • What would you call the ground floor if you were in a country where it is the first floor?
  • Can I say "to join the academy" to mean "to go into academia"?
  • Help is needed with IC identification
  • Why are random integers generated by multiplying by MAX_SAFE_INTEGER not evenly distributed between odd and even?
  • Population size change with equally probable growth and decline
  • When will we land on other planets in our solar system?
  • How can I format these "cases" better?
  • Short story about an inventor that builds a huge tower on the north pole and saves humanity when earth gets destroyed by a passing planet
  • How can I select all edges that form part of a square without a face via python?
  • How is a BJT in the active region able to vary collector current without depending on the collector and emitter resistances?
  • Why is COALESCE not a function?
  • How is HEAP_ALLOCATE related to actual heap size
  • I want to show by simulation that the Wilcoxon test is more robust than the Student test for non-normally distributed data
  • Why is there bronze powder aboard blimps to be used as pyrotechnics?
  • "on a farm" vs "on the farm"
  • Why doesn't Singapore stamp passports?
  • Do big complicated aircraft check the DI?
  • Value of describing game mechanics in a data file instead of hardcoding?

zsh bad assignment export

Navigation Menu

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

IMAGES

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

    zsh bad assignment export

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

    zsh bad assignment export

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

    zsh bad assignment export

  4. [oeasy]python0030_设置路径_export_PATH_zsh_系统路径设置_export

    zsh bad assignment export

  5. [oeasy]python0030_设置路径_export_PATH_zsh_系统路径设置_export

    zsh bad assignment export

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

    zsh bad assignment export

VIDEO

  1. Hum lakh chupaye pyar magar duniya ko pta chal jata hai

  2. Benjamin Franklin Time Traveler

  3. Single mother-building bamboo house in the forest and bad guys come

  4. herobrine VS wither

  5. Is Aloe OK for dogs?

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

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. 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.

  4. Cannot export variable thanks to a script with zsh

    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.

  5. (eval):15 bad assignment when using '=' as alias · Issue #523 · zsh

    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...

  6. 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. You can use SH_WORD_SPLIT to address this difference.

  7. 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.

  8. How to export an environment variable from .sh file in zsh?

    It's a parent- v. child-shell difference. When you run test.sh, a new shell is started to run it; the variable is exported within that shell.That means the new shell is aware of it, and any of its own children. The parent shell, the one you started test.sh from, isn't affected at all (and can't be).. To see the variable in your current shell, you need to source the script instead:

  9. bash

    Using inotify watching .zshrc or by a watcher script that examines the last modification of .zshrc a command is triggered that sends USR1 signal to all zsh processes of the user. Now I just ran it in another shell: ~$ ps -a | awk '$4=="zsh" {print $1}' | xargs kill -SIGUSR1. And the result is:

  10. 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.

  11. Local variables in zsh: what is the equivalent of bash's "export -n" in zsh

    export in zsh is shorthand for typeset -gx, where the attribute g means "global" (as opposed to local to a function) and the attribute x means "exported" (i.e. in the environment). Thus: typeset +x GREP_OPTIONS This also works in ksh and bash. If you never export GREP_OPTIONS in the first place, you don't need to unexport it.. You can also use the indirect, portable way: unsetting a ...

  12. How to make a permanent alias in oh-my-zsh?

    # Set personal aliases, overriding those provided by oh-my-zsh libs, # plugins, and themes. Aliases can be placed here, though oh-my-zsh # users are encouraged to define aliases within the ZSH_CUSTOM folder. # For a full list of active aliases, run alias.

  13. weird ZSH issue (resolving environment variable)

    using ZSH mac latest. In .zshrc I have: export ZSH="~/.oh-my-zsh" that directory exists: cd ~/.oh-my-zsh me@Imac-Retina-2 .oh-my-zsh % ls CODE_OF_CONDUCT.md README.md lib/ oh-my-zsh.sh-SAVED themes/ CONTRIBUTING.md cache/ log/ plugins/ tools/ LICENSE.txt custom/ oh-my-zsh.sh templates/

  14. environment variables

    In zsh, Command Substitution result was performed word splitting if was not enclosed in double quotes. So if your command substitution result contain any whitespace, tab, or newline, the export command will be broken into parts: $ export a=$(echo 1 -2) export: not valid in this context: -2. You need to double quote command substitution to make ...

  15. How to Set Environment Variables in zsh

    To create a permanent environment variable in zsh, you'd have to make changes in the zsh config file. First, open the zshrc file using the following command: nano ~/.zshrc. Go to the end of the file in Nano to write new lines using Alt + / and create new environment variables using the following syntax: export VARIABLE_NAME=VARIABLE_VALUE.

  16. 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 ...

  17. 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.

  18. zsh

    path+=/path/to/go export PATH None of these have worked as expected. As mentioned before, when I do this through the terminal, it works fine, but I want this to be a persistent setting. Any help/input is appreciated. ... after source ~/.zshrc, zsh terminal does not work. Hot Network Questions

  19. bash

    Stack Overflow Public questions & answers; Stack Overflow for Teams Where developers & technologists share private knowledge with coworkers; Talent Build your employer brand ; Advertising Reach developers & technologists worldwide; Labs The future of collective knowledge sharing; About the company

  20. ZSH_HIGHLIGHT_STYLES: assignment to invalid subscript range

    Aliases can be placed here, though oh-my-zsh # users are encouraged to define aliases within the ZSH_CUSTOM folder. # For a full list of active aliases, run `alias`. # # Example aliases # alias zshconfig="mate ~/.zshrc"