exec master..xp_cmdshell Can anyone help me resolve the syntax in the following script? Here @Name and @DBname are declared above. 

exec master..xp_cmdshell 'echo 'alter login QUOTENAME(select @Name) with default_database =  QUOTENAME(select @DBname) &gt;&gt; C:\test\test_script.txt''


Thanks
Try something like this:

Declare @Name varchar(30) = 'Bob'
Declare @DBname sysname = 'TestDb'
Declare @Cmd varchar(4000)
set @Cmd = 'echo ' + 'alter login ' + QUOTENAME(@Name) + ' with default_database = ' + QUOTENAME(@DBname) + ' &gt;&gt; C:\test\test_script.txt'
print @Cmd
exec master..xp_cmdshell @cmd