Code Monkey home page Code Monkey logo

Comments (6)

GiselleSerate avatar GiselleSerate commented on September 26, 2024

If you handle the flag like this, you'll have to figure out how to get the nickname only without the flag preceding it when you try to grep within $current.
For example, if you wrote addalias -s lp="commands" you want to grep for alias lp= to see if it exists, not alias -s lp=.
(It's handleable with regex, but you just have to handle it.)

from myaliases.

aryarm avatar aryarm commented on September 26, 2024

@GiselleSerate, I might be wrong about this, but I think you can just use $1 to get lp="commands". You won't have to parse the entire command, and hence, you won't have to deal with the -s only sometimes existing.

from myaliases.

GiselleSerate avatar GiselleSerate commented on September 26, 2024

That was my first thought, but I think it breaks over spaces, or that was my problem when trying to use numerical arguments even without trying to handle the option.

from myaliases.

aryarm avatar aryarm commented on September 26, 2024

Unless you're doing that because you want to preserve quotes.
Actually, have you ever tried using single quotes instead of double? Ex: alias lp='commands'? I think that might solve the quotes problem.

from myaliases.

GiselleSerate avatar GiselleSerate commented on September 26, 2024

Except that requires the user to type it in that way, and I'm pretty sure the normal alias command lets you use double

from myaliases.

aryarm avatar aryarm commented on September 26, 2024

So I tried to do some experiments with quoting. Take a look at this:
image
This question and this question seem to explain why this happens. To summarize, when a command is executed, bash first evaluates the line that the user typed according to its normal rules. Part of this evaluation includes removing quotes from the parameters, and in the case of double quotes, bash also does some sort of evaluation of the content of the string inside the double quotes such that variables are converted to their actual values (as seen above). In the case of single quotes, the content of the string inside the quotes is interpreted literally.

So it seems clear that single quotes perform differently than double quotes. But what does this have to do with us, as writers of functions that take parameters that may be double or single quoted?
Well, I think it explains that quoting of parameters is a tool of the user, and not something that we should ever handle or deal with. If the user chooses to use single quotes, it's because they want our function to get the literal string that they typed. Likewise, if the user chooses to use double quotes, it's because they want our function to get an evaluated version of their string.
From this stack overflow answer, it seems that this is indeed what happens with the original alias command. I think this also shows that most of the time the user will likely want to use single quotes, not double quotes, when calling alias.

So if your alias command was to use "$1", I don't think it would ever have to deal with spaces. I'm still not sure why you were seeing numerical arguments break over spaces, but it could have been that $1 wasn't surrounded by double quotes in your function?

from myaliases.

Related Issues (20)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.