What is the correct way to assign a function containing chained commands to an alias in .bashrc?

The name of the pictureThe name of the pictureThe name of the pictureClash Royale CLAN TAG#URR8PPP





.everyoneloves__top-leaderboard:empty,.everyoneloves__mid-leaderboard:empty margin-bottom:0;







up vote
0
down vote

favorite












I wish to invoke an alias as follows:



alias_name arg0 arg1 arg2


  • arg0 = filename

  • arg1 = General string

  • arg2 = String representation of last command

The aim is that by calling the alias and passing it arg0, arg1 and arg2 it will call a function which will append the output of the last command to a file, if the file already exists OR if the file does not exist it should create it and insert the output of the last command.



Note: My intention is for the file to be formatted as follows:



String 1 

Command 1

Output of command 1


String i

command i

Output of command i


String N

Command N

Output of command N


I have attempted to achieve this in my .bashrc as follows:



alias alias_name='function_name() printf "nnString_i: $1" >> $0 ; printf "nCommand_i: $2" >> $0; printf "nOutput_of_command_i: $(!!)" >> $0 ;_log_error'


After I added this command to my .bashrc file and did a source ~/.bashrc I then executed the following command:



ls -l


I then invoked my alias:



alias_name arg0 arg1 arg2


But all that I saw in the termainal was:



>


No file was created.



I'm wondering is this the correct method for achieving such results or should I have an alias which calls a script located somewhere else on the system.



NOTE: One constraint I have is portability, I often ssh into other machines and will need to run this command there. This is why I thought alias's might be the correct way to achieve this as I could simply ssh into the machine in question change the name of the ~./bashrc to ~/.bashrc_disabled then scp my bashrc (containing this new alias) into position, run my command and then scp the result file back to my machine, delete the .bashrc and rename the original .bashrc from .bashrc_disabled to .bashrc







share|improve this question















  • 1




    Putting a function definition into an alias seems a bit unneeded. Just define the function before defining the alias, and the call the function in the alias. I doubt you need the alias at all though.
    – Kusalananda
    Aug 3 at 11:27
















up vote
0
down vote

favorite












I wish to invoke an alias as follows:



alias_name arg0 arg1 arg2


  • arg0 = filename

  • arg1 = General string

  • arg2 = String representation of last command

The aim is that by calling the alias and passing it arg0, arg1 and arg2 it will call a function which will append the output of the last command to a file, if the file already exists OR if the file does not exist it should create it and insert the output of the last command.



Note: My intention is for the file to be formatted as follows:



String 1 

Command 1

Output of command 1


String i

command i

Output of command i


String N

Command N

Output of command N


I have attempted to achieve this in my .bashrc as follows:



alias alias_name='function_name() printf "nnString_i: $1" >> $0 ; printf "nCommand_i: $2" >> $0; printf "nOutput_of_command_i: $(!!)" >> $0 ;_log_error'


After I added this command to my .bashrc file and did a source ~/.bashrc I then executed the following command:



ls -l


I then invoked my alias:



alias_name arg0 arg1 arg2


But all that I saw in the termainal was:



>


No file was created.



I'm wondering is this the correct method for achieving such results or should I have an alias which calls a script located somewhere else on the system.



NOTE: One constraint I have is portability, I often ssh into other machines and will need to run this command there. This is why I thought alias's might be the correct way to achieve this as I could simply ssh into the machine in question change the name of the ~./bashrc to ~/.bashrc_disabled then scp my bashrc (containing this new alias) into position, run my command and then scp the result file back to my machine, delete the .bashrc and rename the original .bashrc from .bashrc_disabled to .bashrc







share|improve this question















  • 1




    Putting a function definition into an alias seems a bit unneeded. Just define the function before defining the alias, and the call the function in the alias. I doubt you need the alias at all though.
    – Kusalananda
    Aug 3 at 11:27












up vote
0
down vote

favorite









up vote
0
down vote

favorite











I wish to invoke an alias as follows:



alias_name arg0 arg1 arg2


  • arg0 = filename

  • arg1 = General string

  • arg2 = String representation of last command

The aim is that by calling the alias and passing it arg0, arg1 and arg2 it will call a function which will append the output of the last command to a file, if the file already exists OR if the file does not exist it should create it and insert the output of the last command.



Note: My intention is for the file to be formatted as follows:



String 1 

Command 1

Output of command 1


String i

command i

Output of command i


String N

Command N

Output of command N


I have attempted to achieve this in my .bashrc as follows:



alias alias_name='function_name() printf "nnString_i: $1" >> $0 ; printf "nCommand_i: $2" >> $0; printf "nOutput_of_command_i: $(!!)" >> $0 ;_log_error'


After I added this command to my .bashrc file and did a source ~/.bashrc I then executed the following command:



ls -l


I then invoked my alias:



alias_name arg0 arg1 arg2


But all that I saw in the termainal was:



>


No file was created.



I'm wondering is this the correct method for achieving such results or should I have an alias which calls a script located somewhere else on the system.



NOTE: One constraint I have is portability, I often ssh into other machines and will need to run this command there. This is why I thought alias's might be the correct way to achieve this as I could simply ssh into the machine in question change the name of the ~./bashrc to ~/.bashrc_disabled then scp my bashrc (containing this new alias) into position, run my command and then scp the result file back to my machine, delete the .bashrc and rename the original .bashrc from .bashrc_disabled to .bashrc







share|improve this question











I wish to invoke an alias as follows:



alias_name arg0 arg1 arg2


  • arg0 = filename

  • arg1 = General string

  • arg2 = String representation of last command

The aim is that by calling the alias and passing it arg0, arg1 and arg2 it will call a function which will append the output of the last command to a file, if the file already exists OR if the file does not exist it should create it and insert the output of the last command.



Note: My intention is for the file to be formatted as follows:



String 1 

Command 1

Output of command 1


String i

command i

Output of command i


String N

Command N

Output of command N


I have attempted to achieve this in my .bashrc as follows:



alias alias_name='function_name() printf "nnString_i: $1" >> $0 ; printf "nCommand_i: $2" >> $0; printf "nOutput_of_command_i: $(!!)" >> $0 ;_log_error'


After I added this command to my .bashrc file and did a source ~/.bashrc I then executed the following command:



ls -l


I then invoked my alias:



alias_name arg0 arg1 arg2


But all that I saw in the termainal was:



>


No file was created.



I'm wondering is this the correct method for achieving such results or should I have an alias which calls a script located somewhere else on the system.



NOTE: One constraint I have is portability, I often ssh into other machines and will need to run this command there. This is why I thought alias's might be the correct way to achieve this as I could simply ssh into the machine in question change the name of the ~./bashrc to ~/.bashrc_disabled then scp my bashrc (containing this new alias) into position, run my command and then scp the result file back to my machine, delete the .bashrc and rename the original .bashrc from .bashrc_disabled to .bashrc









share|improve this question










share|improve this question




share|improve this question









asked Aug 3 at 11:07









MarkMark

7717




7717







  • 1




    Putting a function definition into an alias seems a bit unneeded. Just define the function before defining the alias, and the call the function in the alias. I doubt you need the alias at all though.
    – Kusalananda
    Aug 3 at 11:27












  • 1




    Putting a function definition into an alias seems a bit unneeded. Just define the function before defining the alias, and the call the function in the alias. I doubt you need the alias at all though.
    – Kusalananda
    Aug 3 at 11:27







1




1




Putting a function definition into an alias seems a bit unneeded. Just define the function before defining the alias, and the call the function in the alias. I doubt you need the alias at all though.
– Kusalananda
Aug 3 at 11:27




Putting a function definition into an alias seems a bit unneeded. Just define the function before defining the alias, and the call the function in the alias. I doubt you need the alias at all though.
– Kusalananda
Aug 3 at 11:27















active

oldest

votes











Your Answer







StackExchange.ready(function()
var channelOptions =
tags: "".split(" "),
id: "106"
;
initTagRenderer("".split(" "), "".split(" "), channelOptions);

StackExchange.using("externalEditor", function()
// Have to fire editor after snippets, if snippets enabled
if (StackExchange.settings.snippets.snippetsEnabled)
StackExchange.using("snippets", function()
createEditor();
);

else
createEditor();

);

function createEditor()
StackExchange.prepareEditor(
heartbeatType: 'answer',
convertImagesToLinks: false,
noModals: false,
showLowRepImageUploadWarning: true,
reputationToPostImages: null,
bindNavPrevention: true,
postfix: "",
onDemand: true,
discardSelector: ".discard-answer"
,immediatelyShowMarkdownHelp:true
);



);








 

draft saved


draft discarded


















StackExchange.ready(
function ()
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2funix.stackexchange.com%2fquestions%2f460301%2fwhat-is-the-correct-way-to-assign-a-function-containing-chained-commands-to-an-a%23new-answer', 'question_page');

);

Post as a guest



































active

oldest

votes













active

oldest

votes









active

oldest

votes






active

oldest

votes










 

draft saved


draft discarded


























 


draft saved


draft discarded














StackExchange.ready(
function ()
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2funix.stackexchange.com%2fquestions%2f460301%2fwhat-is-the-correct-way-to-assign-a-function-containing-chained-commands-to-an-a%23new-answer', 'question_page');

);

Post as a guest













































































Popular posts from this blog

How to check contact read email or not when send email to Individual?

Bahrain

Postfix configuration issue with fips on centos 7; mailgun relay